This forum is in archive mode. You will not be able to post new content.

Author Topic: Moving car, school project!  (Read 16395 times)

0 Members and 1 Guest are viewing this topic.

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: Moving car, school project!
« Reply #15 on: March 03, 2013, 12:57:49 PM »

Day 22.So, I found the "perfect" battery for my car. The V4034PX varta 6V, 100 mAh.


The motor takes 2,4V to 6V and is on 250 mA.
The Transistor in the picture is just random, idk what I should use. (Help is appreciated, the Transistor is Tesla BUF74).
Found the battery while looking around for some old computers to make into a server for me and my family, found only one, but I think there are more where I found it.

::EDIT:: wow, totally failed with that picture, sorry if you saw it XD.
« Last Edit: March 03, 2013, 01:29:54 PM by WirelessDesert »
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: Moving car, school project!
« Reply #16 on: March 19, 2013, 10:30:58 PM »
Day WOOHOO! (#idk)


So yesterday, I bought the arduino motor shield just because I wanted to buy it. (Sorry techb :P )


Made some coding while in school, and I've come up with this.
http://pastebin.com/CTxQD9Hm
Much of it is commented, but feel free to ask about anything you want.


Anyway, here is a picture of my current setup.







Clean desktop FTW!




Thinking about using one TSOP38238 38KHz IR receivers to control it, I'll just have to figure out the protocol of a matching remote control.
Oh, And I'm wondering if I need to use diodes between the motors two pins so I don't get the drawback current thingy. Think I should do it.
« Last Edit: March 19, 2013, 10:42:28 PM by WirelessDesert »
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Moving car, school project!
« Reply #17 on: March 19, 2013, 10:34:07 PM »
Day WOOHOO! (#idk)


So yesterday, I bought the arduino motor shield just because I wanted to buy it. (Sorry techb :P)


Made some coding while in school, and I've come up with this.
http://pastebin.com/CTxQD9Hm
Much of it is commented, but feel free to ask about anything you want.


Anyway, here is a picture of my current setup.







Clean desktop FTW!




Thinking about using one TSOP38238 38KHz IR receivers to control it, I'll just have to figure out the protocol of a matching remote control.
Nice one :D. Now you can cool the car with a computer fan.
~Factionwars

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: Moving car, school project!
« Reply #18 on: April 01, 2013, 06:43:19 PM »
Day IDK again(sorry).


So, today I really tried to make my car wirelessly steerable.
Using the library IrRemote (https://github.com/shirriff/Arduino-IRremote) for arduino.
Been trying to figure out this old remote I found at home.




It works surprisingly well, the only problem is that it returns a 12th bit of value 1(true) every second press.
First Press on button 1:

Code: [Select]
1Second Press on Button 1:
Code: [Select]
100000000001It even keeps the toggle for a new button:
First Press on Button 2:
Code: [Select]
10Second Press on Button 4:

Code: [Select]
100000000100

Should be easily filtered out by removing every 12th bit with a bitwise subtraction.
However, the wierd thing happens when I hold down a button.
Sometimes, it returns things like this. (Held down The button 3 for 10 seconds):
Code: [Select]

11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11100100010000000000101100010100
11
11100100010000000000101100010100
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11100100010000000000101100010100
11
11
11
11
11
11
11
11
11
11
11
11
11
11
11
Have no idea what it is, but I'll either filter out it by doing an OR on the if statements, or to just ignore every input over 13 bits. However, I think it is a "placeholder" or "marker" for something, to indicate a cycle maybe.
And oh, here is my code, no comments on it because it is all on the website of the library I'm using. http://pastebin.com/iR3Bau6G
::UPDATE::
I think the error stated above is caused by bounces in the remote. Or that Serial.println() isn't fast enough. Have no idea, maybe it's even a problem with the IRremote library. I should maybe try another remote and see. (Not a sony one though, heard that it sends the info three times so that would just be a mess.)
« Last Edit: April 01, 2013, 09:23:45 PM by WirelessDesert »
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline WirelessDesert

  • Knight
  • **
  • Posts: 356
  • Cookies: 10
  • I think...
    • View Profile
Re: Moving car, school project!
« Reply #19 on: April 10, 2013, 04:53:40 PM »
Day (#fill in the day here.)


So, today, I've fixed it all, the only thing missing is the car chassi.
Other then that, it's finished. Some pictures and maybe a video will come soon.
Final Code:
Code: (cpp) [Select]
#include <IRremote.h>




struct WheelStruct {
  int mspeed; // Max is 255
  int mForward; // Is forward or not.
  const int speedPinD; // Digital, PWM
  const int directionPinD; // Digital
  const int brakePinD; // Digital (I've got no clue what this is.)
  const int currentSensingPinA; // Analog


};


const int ZERO = 0;
const int MAX = 255;
const int HALF = 123+40;
const int QUARTER = 95;
boolean CLOCKWISE = true;


WheelStruct motorA = {
  ZERO, true, 3, 12, 9, 0}; // Motor A is left
WheelStruct motorB = {
  ZERO, true, 11, 13, 8, 1}; // Motor B is right.


const int ledLampAPin = 4;
const int ledLampBPin = 5;
boolean stopped = false;


boolean toggle = false;
long previousMillis = 0;
long interval = 1000;


int recvPin = 2;
IRrecv irrecv(recvPin);
decode_results results;




void setup(){


  Serial.begin(9600);


  pinMode(motorA.speedPinD, OUTPUT);
  pinMode(motorB.speedPinD, OUTPUT);


  pinMode(motorA.directionPinD, OUTPUT);
  pinMode(motorB.directionPinD, OUTPUT);


  pinMode(motorA.brakePinD, OUTPUT);
  pinMode(motorB.brakePinD, OUTPUT);


  pinMode(motorA.currentSensingPinA, INPUT);
  pinMode(motorB.currentSensingPinA, INPUT);


  irrecv.enableIRIn();


}


void loop(){


  if(gotKey()){
    while(getKey() >= 3000){
      softReset();
    }
    switch(getKey()){
    case 5:
      motorTurn(ZERO);
      Serial.println("Stopped");
      break;
    case 2:
      motorTurn(MAX);
      Serial.println("Forwards.");
      break;
    case 4:
      motorTurn(MAX, -MAX); // If it doesn't work, try (MAX, ZERO)
      Serial.println("Left.");
      break;
    case 6:
      motorTurn(-MAX, MAX); // Again, if not, try (ZERO, MAX)
      Serial.println("Right.");
      break;
    case 8:
      motorTurn(-MAX);
      Serial.println("Backwards.");
      break;
    case 1:
      motorTurn(HALF, MAX);
      Serial.println("Left, forward.");
      break;
    case 3:
      motorTurn(MAX, HALF-40);
      Serial.println("Right, forward.");
      break;
    case 7:
      motorTurn(-HALF, -MAX);
      Serial.println("Left, backwards.");
      break;
    case 9:
      motorTurn(-MAX, -HALF);
      Serial.println("Right, backwards.");
      break;
    case 12:
      if(stopped)
        unlock();
      else{
        brake();
        stopped = true;
      }
      delay(100);
    default:
      break;
    }
    irrecv.resume();
  }
}


/*                                   FUNCTIONS STARTS HERE                            */


/*                                                                                    *
 *     switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin)      *
 *                                                                                    */


void switchWheelDirection(int mDirPin){
  if(digitalRead(mDirPin) == HIGH)
    digitalWrite(mDirPin, LOW);
  else
    digitalWrite(mDirPin, HIGH);
}




/*                                                                                    *
 *     writeMotor - replaces the analogWrite() function, positive values = forward    *
 *     negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100     *
 *                                                                                    */
void motorWrite(char motor, int mspeed){
  if (mspeed >= 0 && mspeed <= 255){


    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, HIGH);
      analogWrite(motorA.speedPinD, mspeed);
      motorA.mForward = true;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, HIGH);
      analogWrite(motorB.speedPinD, mspeed);
      motorB.mForward = true;
    }
  }
  else if(mspeed >= -255 && mspeed <= 0){
    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, LOW);
      analogWrite(motorA.speedPinD, abs(mspeed));
      motorA.mForward = false;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, LOW);
      analogWrite(motorB.speedPinD, abs(mspeed));
      motorB.mForward = false;
    }
  }


}




/*                                                                                    *
 *                motorTurn - change the speed of each wheel idividually.             *
 *                                                                                    */
void motorTurn(int lSpeed, int rSpeed){
  motorWrite('A', lSpeed);
  motorWrite('B', rSpeed);
}
void motorTurn(int mspeed){
  motorWrite('A', mspeed);
  motorWrite('B', mspeed);
}
/*                                                                                    *
 *  spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise   *
 *            use forward or brake to disable. Use CLOCKWISE/!CLOCKWISE               *
 *                                                                                    */
void spinAround(boolean clockwise){
  if(clockwise == true){
    // Makes the other wheel go forward, the other one backward.


  }
}
/*                                                                                    *
 *                                 brake - stops all motors                           *
 *                                                                                    */
void brake(){
  digitalWrite(motorA.brakePinD, HIGH);
  digitalWrite(motorB.brakePinD, HIGH);
}


/*                                                                                    *
 *                       unlock - reverses the effects of brake                       *
 *                                                                                    */
void unlock(){
  digitalWrite(motorA.brakePinD, LOW);
  digitalWrite(motorB.brakePinD, LOW);
}


/*                                                                                    *
 *         gotKey - "lazydog" for IRrecv irrecv.decode(decode_results &results)       *
 *                                                                                    */
boolean gotKey(){
  return irrecv.decode(&results);
}


/*                                                                                    *
 *      getKey - "lazydog" for decode_results results.value & 2047                    *
 *                                                                                    */
int getKey(){
  return results.value & 2047; // ANDing with 2047 for making the 12th bit disappear.
}
/*                                                                                    *
 *          softReset - macro for soft reseting the arduino, doesn't wipe             *
 *                                                                                    */
void softReset(){
  asm volatile ("  jmp 0");
}
/*                                   FUNCTIONS ENDS HERE                              */
























http://pastebin.com/Cg8LBrRy
Check out my arduino project: Moving car - School project!
"I'm like current, I always take the easiest route."

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
Re: Moving car, school project!
« Reply #20 on: April 10, 2013, 05:02:14 PM »
Day (#fill in the day here.)


So, today, I've fixed it all, the only thing missing is the car chassi.
Other then that, it's finished. Some pictures and maybe a video will come soon.
Final Code:
Code: (cpp) [Select]
#include <IRremote.h>




struct WheelStruct {
  int mspeed; // Max is 255
  int mForward; // Is forward or not.
  const int speedPinD; // Digital, PWM
  const int directionPinD; // Digital
  const int brakePinD; // Digital (I've got no clue what this is.)
  const int currentSensingPinA; // Analog


};


const int ZERO = 0;
const int MAX = 255;
const int HALF = 123+40;
const int QUARTER = 95;
boolean CLOCKWISE = true;


WheelStruct motorA = {
  ZERO, true, 3, 12, 9, 0}; // Motor A is left
WheelStruct motorB = {
  ZERO, true, 11, 13, 8, 1}; // Motor B is right.


const int ledLampAPin = 4;
const int ledLampBPin = 5;
boolean stopped = false;


boolean toggle = false;
long previousMillis = 0;
long interval = 1000;


int recvPin = 2;
IRrecv irrecv(recvPin);
decode_results results;




void setup(){


  Serial.begin(9600);


  pinMode(motorA.speedPinD, OUTPUT);
  pinMode(motorB.speedPinD, OUTPUT);


  pinMode(motorA.directionPinD, OUTPUT);
  pinMode(motorB.directionPinD, OUTPUT);


  pinMode(motorA.brakePinD, OUTPUT);
  pinMode(motorB.brakePinD, OUTPUT);


  pinMode(motorA.currentSensingPinA, INPUT);
  pinMode(motorB.currentSensingPinA, INPUT);


  irrecv.enableIRIn();


}


void loop(){


  if(gotKey()){
    while(getKey() >= 3000){
      softReset();
    }
    switch(getKey()){
    case 5:
      motorTurn(ZERO);
      Serial.println("Stopped");
      break;
    case 2:
      motorTurn(MAX);
      Serial.println("Forwards.");
      break;
    case 4:
      motorTurn(MAX, -MAX); // If it doesn't work, try (MAX, ZERO)
      Serial.println("Left.");
      break;
    case 6:
      motorTurn(-MAX, MAX); // Again, if not, try (ZERO, MAX)
      Serial.println("Right.");
      break;
    case 8:
      motorTurn(-MAX);
      Serial.println("Backwards.");
      break;
    case 1:
      motorTurn(HALF, MAX);
      Serial.println("Left, forward.");
      break;
    case 3:
      motorTurn(MAX, HALF-40);
      Serial.println("Right, forward.");
      break;
    case 7:
      motorTurn(-HALF, -MAX);
      Serial.println("Left, backwards.");
      break;
    case 9:
      motorTurn(-MAX, -HALF);
      Serial.println("Right, backwards.");
      break;
    case 12:
      if(stopped)
        unlock();
      else{
        brake();
        stopped = true;
      }
      delay(100);
    default:
      break;
    }
    irrecv.resume();
  }
}


/*                                   FUNCTIONS STARTS HERE                            */


/*                                                                                    *
 *     switchWheelDirection - toggles the state of mDirPin (Motor Direction Pin)      *
 *                                                                                    */


void switchWheelDirection(int mDirPin){
  if(digitalRead(mDirPin) == HIGH)
    digitalWrite(mDirPin, LOW);
  else
    digitalWrite(mDirPin, HIGH);
}




/*                                                                                    *
 *     writeMotor - replaces the analogWrite() function, positive values = forward    *
 *     negative values = backwards, for ex "mspeed = 100" == motorX.mspeed == 100     *
 *                                                                                    */
void motorWrite(char motor, int mspeed){
  if (mspeed >= 0 && mspeed <= 255){


    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, HIGH);
      analogWrite(motorA.speedPinD, mspeed);
      motorA.mForward = true;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, HIGH);
      analogWrite(motorB.speedPinD, mspeed);
      motorB.mForward = true;
    }
  }
  else if(mspeed >= -255 && mspeed <= 0){
    if(motor == 'A'){
      digitalWrite(motorA.directionPinD, LOW);
      analogWrite(motorA.speedPinD, abs(mspeed));
      motorA.mForward = false;
    }
    if(motor == 'B'){
      digitalWrite(motorB.directionPinD, LOW);
      analogWrite(motorB.speedPinD, abs(mspeed));
      motorB.mForward = false;
    }
  }


}




/*                                                                                    *
 *                motorTurn - change the speed of each wheel idividually.             *
 *                                                                                    */
void motorTurn(int lSpeed, int rSpeed){
  motorWrite('A', lSpeed);
  motorWrite('B', rSpeed);
}
void motorTurn(int mspeed){
  motorWrite('A', mspeed);
  motorWrite('B', mspeed);
}
/*                                                                                    *
 *  spinAround - spins the car clockwise/anti-clockwise depending on bool clockwise   *
 *            use forward or brake to disable. Use CLOCKWISE/!CLOCKWISE               *
 *                                                                                    */
void spinAround(boolean clockwise){
  if(clockwise == true){
    // Makes the other wheel go forward, the other one backward.


  }
}
/*                                                                                    *
 *                                 brake - stops all motors                           *
 *                                                                                    */
void brake(){
  digitalWrite(motorA.brakePinD, HIGH);
  digitalWrite(motorB.brakePinD, HIGH);
}


/*                                                                                    *
 *                       unlock - reverses the effects of brake                       *
 *                                                                                    */
void unlock(){
  digitalWrite(motorA.brakePinD, LOW);
  digitalWrite(motorB.brakePinD, LOW);
}


/*                                                                                    *
 *         gotKey - "lazydog" for IRrecv irrecv.decode(decode_results &results)       *
 *                                                                                    */
boolean gotKey(){
  return irrecv.decode(&results);
}


/*                                                                                    *
 *      getKey - "lazydog" for decode_results results.value & 2047                    *
 *                                                                                    */
int getKey(){
  return results.value & 2047; // ANDing with 2047 for making the 12th bit disappear.
}
/*                                                                                    *
 *          softReset - macro for soft reseting the arduino, doesn't wipe             *
 *                                                                                    */
void softReset(){
  asm volatile ("  jmp 0");
}
/*                                   FUNCTIONS ENDS HERE                              */
























http://pastebin.com/Cg8LBrRy
Nice :)
~Factionwars

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.