Go Down

Topic: Simple RC Car Programming Issue (Read 1 time) previous topic - next topic

Hello Arduino world,

I am a newbie at working with the Arduino and I required some assistance with a projec that I am working on.

This is the code I am using:
Code: [Select]


int ir_pin = 2; //Sensor pin 1 wired through a 220 ohm resistor
int led_pin = 13;
int ledpin = 12;   //"Ready to Receive" flag, not needed but nice
int debug = 1;   //Serial connection must be started to debug
int start_bit = 2000; //Start bit threshold (Microseconds)
int bin_1 = 1000;   //Binary 1 threshold (Microseconds)
int bin_0 = 400;     //Binary 0 threshold (Microseconds)


void setup() {
pinMode(led_pin, OUTPUT); //This shows when we're ready to receive
pinMode(ir_pin, INPUT);
pinMode(ledpin, OUTPUT);
digitalWrite(led_pin, LOW); //not ready yet
Serial.begin(9600);
}

void loop() {
int key = getIRKey();  //Fetch the key
if (key != -1) {
  Serial.print("Key Recieved: ");
  Serial.println(key);

}

 if (key == 16) {
  digitalWrite(ledpin, HIGH);
  delay(10);


 }
else {
  digitalWrite (ledpin,LOW);
}
}



int getIRKey() {
int data[12];

digitalWrite(led_pin, HIGH);     //Ok, i'm ready to recieve
while(pulseIn(ir_pin, LOW) < 2200) { //Wait for a start bit
}
data[0] = pulseIn(ir_pin, LOW); //Start measuring bits, I only want low pulses
data[1] = pulseIn(ir_pin, LOW);
data[2] = pulseIn(ir_pin, LOW);
data[3] = pulseIn(ir_pin, LOW);
data[4] = pulseIn(ir_pin, LOW);
data[5] = pulseIn(ir_pin, LOW);
data[6] = pulseIn(ir_pin, LOW);
data[7] = pulseIn(ir_pin, LOW);
data[8] = pulseIn(ir_pin, LOW);
data[9] = pulseIn(ir_pin, LOW);
data[10] = pulseIn(ir_pin, LOW);
data[11] = pulseIn(ir_pin, LOW);
digitalWrite(led_pin, LOW);

if(debug == 1) {
 
}
for(int i=0;i<=11;i++) {     //Parse them
  if (debug == 1) {
 
  }
  if(data[i] > bin_1) {     //is it a 1?
data[i] = 1;
  }  else {
if(data[i] > bin_0) {     //is it a 0?
  data[i] = 0;
} else {
 data[i] = 2;   //Flag the data as invalid; I don't know what it is!
}
  }
}

for(int i=0;i<=11;i++) {     //Pre-check data for errors
  if(data[i] > 1) {
return -1;   //Return -1 on invalid data
  }
}

int result = 0;
int seed = 1;
for(int i=11;i>=0;i--) {    //Convert bits to integer
  if(data[i] == 1) {
result += seed;
  }
  seed = seed * 2;
}
 
return result;     //Return key number
}





Basically, what I am attempting to do is to light up an LED every time I press a button on a remote with an IR sensor. The signal is received by the Arduino, however, I don't know // how to make it work the way I'd like to. When I press a button on the remote, the LED turns on but I want the LED to turn back off after I let go of the button; this code will later be assigned to a motor in which when I press the button, the RC car will go forward and when I let go of the button, the RC car will stop. As of now, I have to press a different button to turn off the LED. Please check the void loop code and see what's wrong. 


wildbill

That getIRKey routine blocks waiting for a key to be pressed. Thus the led stays on until you press another.

#2
May 15, 2012, 06:03 pm Last Edit: May 15, 2012, 06:07 pm by CallMeShady Reason: 1
How would you make it that when you press the button (LED turns and stays on) and then when you let go of the button (LED goes off)?

UPDATED CODE:
Code: [Select]
int ir_pin = 2;
int led_pin = 13;

int ledLEFT = 12;
int ledFORWARD = 11;
int ledBACK = 10;
int ledRIGHT = 8;

int debug = 1;  
int start_bit = 2000;
int bin_1 = 1000;
int bin_0 = 400;    


void setup() {
pinMode(led_pin, OUTPUT);
pinMode(ir_pin, INPUT);

pinMode(ledLEFT, OUTPUT);
pinMode(ledFORWARD, OUTPUT);
pinMode(ledBACK, OUTPUT);
pinMode(ledRIGHT, OUTPUT);

digitalWrite(led_pin, LOW);

Serial.begin(9600);
}

void loop() {
int key = getIRKey();
if (key != -1) {
   Serial.print("Key Received: ");
   Serial.println(key);
}

  if (key == 16) {                           //To go left + forward.
   digitalWrite(ledLEFT, HIGH);
   digitalWrite(ledFORWARD, HIGH);
   digitalWrite(ledBACK, LOW);
   digitalWrite(ledRIGHT, LOW);
   delay(100);
   digitalWrite(ledFORWARD, LOW);
  }
 
  if (key == xxxx) {                         //To go forward.                         
   digitalWrite(ledLEFT, LOW);
   digitalWrite(ledFORWARD, HIGH);
   digitalWrite(ledBACK, LOW);
   digitalWrite(ledRIGHT, LOW);
   delay(100);
   digitalWrite(ledFORWARD, LOW);
  }
 
  if (key == 1040) {                         //To go right + forward.
   digitalWrite(ledLEFT, LOW);
   digitalWrite(ledFORWARD, HIGH);
   digitalWrite(ledBACK, LOW);
   digitalWrite(ledRIGHT, HIGH);
   delay(100);
   digitalWrite(ledFORWARD, LOW);
  }
 
  if (key == 2064) {                         //To go left + back.                         
   digitalWrite(ledLEFT, HIGH);
   digitalWrite(ledFORWARD, LOW);
   digitalWrite(ledBACK, HIGH);
   digitalWrite(ledRIGHT, LOW);
   delay(100);
   digitalWrite(ledBACK, LOW);
  }
 
  if (key == 528) {                          //To go back.
   digitalWrite(ledLEFT, LOW);
   digitalWrite(ledFORWARD, LOW);
   digitalWrite(ledBACK, HIGH);
   digitalWrite(ledRIGHT, LOW);
   delay(100);
   digitalWrite(ledBACK, LOW);
  }
 
  if (key == xxxx) {                         //To go right + back.
   digitalWrite(ledLEFT, LOW);
   digitalWrite(ledFORWARD, LOW);
   digitalWrite(ledBACK, HIGH);
   digitalWrite(ledRIGHT, HIGH);
   delay(100);
   digitalWrite(ledBACK, LOW);
  }
 
  if (key == xxxx) {                         //To stop.
   digitalWrite(ledLEFT, LOW);
   digitalWrite(ledFORWARD, LOW);
   digitalWrite(ledBACK, LOW);
   digitalWrite(ledRIGHT, LOW);
   delay(100);
  }

}


int getIRKey() {
int data[12];

digitalWrite(led_pin, HIGH);     
while(pulseIn(ir_pin, LOW) < 2200) {
}

data[0] = pulseIn(ir_pin, LOW);
data[1] = pulseIn(ir_pin, LOW);
data[2] = pulseIn(ir_pin, LOW);
data[3] = pulseIn(ir_pin, LOW);
data[4] = pulseIn(ir_pin, LOW);
data[5] = pulseIn(ir_pin, LOW);
data[6] = pulseIn(ir_pin, LOW);
data[7] = pulseIn(ir_pin, LOW);
data[8] = pulseIn(ir_pin, LOW);
data[9] = pulseIn(ir_pin, LOW);
data[10] = pulseIn(ir_pin, LOW);
data[11] = pulseIn(ir_pin, LOW);

digitalWrite(led_pin, LOW);

if(debug == 1) {
}

for(int i=0;i<=11;i++) {    
   if (debug == 1) { 
   }
   
   if(data[i] > bin_1) {    
data[i] = 1;
   } else {
if(data[i] > bin_0) {      
   data[i] = 0;
} else {
  data[i] = 2;  
}
   }
}

for(int i=0;i<=11;i++) {      
   if(data[i] > 1) {
return -1;
   }
}

int result = 0;
int seed = 1;
for(int i=11;i>=0;i--) {  
   if(data[i] == 1) {
result += seed;
   }
   seed = seed * 2;
}
 
return result;  
}

yosler

Jeremy Blum has an Arduino tutorial series, and episode 2 is mostly on the LEDs. @ the end of the video, he shows you how to toggle on/off.
http://www.youtube.com/watch?v=_LCCGFSMOr4&feature=BFa&list=SPA567CE235D39FA84
I'm 13 and I'll frequently be wordy, not writing to the point.
But don't worry, I know somethings about electronics.

yosler

I fixed up your code from any errors and made it simpler to understand:
Code: [Select]
int ir_pin = 2; //Sensor pin 1 wired through a 220 ohm resistor
int led_pin = 13;        // built in LED

int ledLEFT = 12;
int ledFORWARD = 11;
int ledBACK = 10;
int ledRIGHT = 8;

int debug = 1;  
int start_bit = 2000;
int bin_1 = 1000;
int bin_0 = 400;    


void setup() {
pinMode(led_pin, OUTPUT);
pinMode(ir_pin, INPUT);

pinMode(ledLEFT, OUTPUT);
pinMode(ledFORWARD, OUTPUT);
pinMode(ledBACK, OUTPUT);
pinMode(ledRIGHT, OUTPUT);

digitalWrite(led_pin, LOW);

Serial.begin(9600);
}

void loop()
{
   int key = getIRKey();
   if (key != -1)
   {
     Serial.print("Key Received: ");
     Serial.println(key);
   }
   
   switch (key)
   {
     case 16:                      // left + forward
       LF();
       break;
       
     //case xxxx:                    // forward
     //  f();
     //  break;
       
     case 1040:                    // right + forward
       RF();
       break;
       
     case 2064:                    // left + back
       LB();
       break;
       
     case 528:                     // back
       B();
       break;
       
     //case xxxx:                    // right + back
       RB();
       break;
       
     //case xxxx:                    // stop
     //  S();
     //  break;
       
     case -1:                      // no key received
       break;
   }
}

  void LF()                          //To go left + forward.
  {                           
    digitalWrite(ledLEFT, HIGH);
    digitalWrite(ledFORWARD, HIGH);
    digitalWrite(ledBACK, LOW);
    digitalWrite(ledRIGHT, LOW);
    delay(100);
    digitalWrite(ledFORWARD, LOW);
  }
 
  void f()                     //To go forward.     
  {
     digitalWrite(ledLEFT, LOW);
     digitalWrite(ledFORWARD, HIGH);
     digitalWrite(ledBACK, LOW);
     digitalWrite(ledRIGHT, LOW);
     delay(100);
     digitalWrite(ledFORWARD, LOW);
  }
 
  void RF()                      //To go right + forward.
  {
     digitalWrite(ledLEFT, LOW);
     digitalWrite(ledFORWARD, HIGH);
     digitalWrite(ledBACK, LOW);
     digitalWrite(ledRIGHT, HIGH);
     delay(100);
     digitalWrite(ledFORWARD, LOW);
  }
 
  void LB() {                         //To go left + back.                         
    digitalWrite(ledLEFT, HIGH);
    digitalWrite(ledFORWARD, LOW);
    digitalWrite(ledBACK, HIGH);
    digitalWrite(ledRIGHT, LOW);
    delay(100);
    digitalWrite(ledBACK, LOW);
  }
 
  void B() {                          //To go back.
    digitalWrite(ledLEFT, LOW);
    digitalWrite(ledFORWARD, LOW);
    digitalWrite(ledBACK, HIGH);
    digitalWrite(ledRIGHT, LOW);
    delay(100);
    digitalWrite(ledBACK, LOW);
  }
 
  void RB() {                         //To go right + back.
    digitalWrite(ledLEFT, LOW);
    digitalWrite(ledFORWARD, LOW);
    digitalWrite(ledBACK, HIGH);
    digitalWrite(ledRIGHT, HIGH);
    delay(100);
    digitalWrite(ledBACK, LOW);
  }
 
  void S() {                         //To stop.
    digitalWrite(ledLEFT, LOW);
    digitalWrite(ledFORWARD, LOW);
    digitalWrite(ledBACK, LOW);
    digitalWrite(ledRIGHT, LOW);
    delay(100);
  }


int getIRKey() {
  int data[12];
 
  digitalWrite(led_pin, HIGH);     
  while(pulseIn(ir_pin, LOW) < 2200) {
  }

data[0] = pulseIn(ir_pin, LOW);
data[1] = pulseIn(ir_pin, LOW);
data[2] = pulseIn(ir_pin, LOW);
data[3] = pulseIn(ir_pin, LOW);
data[4] = pulseIn(ir_pin, LOW);
data[5] = pulseIn(ir_pin, LOW);
data[6] = pulseIn(ir_pin, LOW);
data[7] = pulseIn(ir_pin, LOW);
data[8] = pulseIn(ir_pin, LOW);
data[9] = pulseIn(ir_pin, LOW);
data[10] = pulseIn(ir_pin, LOW);
data[11] = pulseIn(ir_pin, LOW);

digitalWrite(led_pin, LOW);

if (debug == 1) {
}

for (int i=0; i<=11; i++) {    
   if (debug == 1) { 
   }
   
   if(data[i] > bin_1) {    
data[i] = 1;
        }
   else {
if(data[i] > bin_0) {      
   data[i] = 0; }
         else {
   data[i] = 2;  
}
   }
}

for (int i = 0; i <= 11; i++)
{      
   if (data[i] > 1)
   {
     return -1;
   }
}

int result = 0;
int seed = 1;
for(int i = 11; i >= 0; i--)
{  
   if(data[i] == 1)
   {
     result += seed;
   }
   seed = seed * 2;
}
return result;
}
I'm 13 and I'll frequently be wordy, not writing to the point.
But don't worry, I know somethings about electronics.

Go Up