Instructables

Serial Controlled Arduino RC Car buggy please help?

I have a RC Car that I have Converted to be controlled via Serial sent over Xbees to a custom board running a atmega8

My problem is that sometimes you power it on and it will wiz off by its self or do random stuff i have no idea whats causing it

here is my code

[code]

char val;

int forward = 8;
int back =  7;
int left= 6;
int right= 5;
int power = 13;
int NEON = 9;
int HORN = 10;

void setup() {
  pinMode(forward, OUTPUT);
  pinMode(back, OUTPUT);
  pinMode(left, OUTPUT);
  pinMode(right, OUTPUT);
  pinMode(power, OUTPUT);
  pinMode(NEON, OUTPUT);
  pinMode(HORN, OUTPUT);
 
  digitalWrite(power, HIGH);
  delay(500);
  digitalWrite(power, LOW);
  delay(500); 
  digitalWrite(power, HIGH);
  delay(500); 
  digitalWrite(power, LOW);
  delay(500);
  digitalWrite(power, HIGH);
  delay(500);
  digitalWrite(power, LOW);
  delay(500); 
  digitalWrite(power, HIGH);
  delay(500); 
  digitalWrite(power, LOW);

  Serial.begin(19200);
  Serial.print("CAR AWAITING COMMANDS");
}

void loop() {

  if (Serial.available()>0)
  {                             
    val = Serial.read();
    if (val == 'A')
    {
      digitalWrite(forward, HIGH);
      digitalWrite(back, LOW);
    }
    else if (val == 'B')
    {
      digitalWrite(forward, LOW);
      digitalWrite(back, HIGH);
    }
    else if (val == 'H')
    {
      digitalWrite(NEON, HIGH);
    }
    else if (val == 'Z')
    {
      digitalWrite(NEON, LOW);
    }   
    else if (val == 'Y')
    {
      digitalWrite(HORN, HIGH);
    }
    else if (val == 'X')
    {
      digitalWrite(HORN, LOW);
    }       
    else if (val == 'L')
    {                            
      digitalWrite(right, LOW);
      digitalWrite(left, HIGH);
    }
    else if (val == 'R')
    {
      digitalWrite(right, HIGH);
      digitalWrite(left, LOW);
    }
    else if (val == 'C')
    {                           
      digitalWrite(left, LOW);
    }
    else if (val == 'E')
    {
      digitalWrite(right, LOW);
    }
    else if (val == 'Q')
    {
      digitalWrite(forward, LOW);
    }
    else if (val == 'W')
    {
      digitalWrite(back, LOW);
    }
    else if (val == 'S')
    {
      digitalWrite(forward, LOW);
      digitalWrite(back, LOW);
      digitalWrite(left, LOW);
      digitalWrite(right, LOW);
      digitalWrite(power,HIGH);
      delay(500);
      digitalWrite(power,LOW);
    }
  }
  else
  {
    val = 'S';
  }
}
[/code]

I bet if you change;

char val;

To;

char val='S';

you won't have that problem anymore. Otherwise val may pick up any value that may be stored in that address space at that time. So it could still have a value stored there from the last time you ran the arduino.
Jsmxbox (author)  mpilchfamily1 year ago
Thanks will give it ago

also have another problem when it does work it randomly keeps resetting but continues running the last cmd so if its going forward resets and just runs off haha
Make sure you initialise all your variables.
Simple dc motors are fearsomely electrically noisy and can/will interfere with any micro close by. Use suppression capacitors on power lines, motor brushes and close to th micro.