Go Down

Topic: Coding Error in My Sketch - Any Ideas? (Read 443 times) previous topic - next topic

GSOON

Hi!

This is my first time posting so if I'm not supposed to ask this question or it should be someplace else, do please let me know.

I have an Arduino Uno R3 and also a servo and an IR receiver.  I can press buttons on my remote and it writes the "Left" or "Right" as below.  Also, when I upload the code below, the servo turns to the right and sticks there, as if trying to go further.  What I am trying to do is click the left or right button on my remote and make the servo spin one way or the other. 

I'm certain that I am misunderstanding something simple here. . .    Can anyone suggest changes?

Thanks so much!

-Gary
=======  SNIP =====
#include <Servo.h>
#include <IRremote.h> // use the library
Servo myservo;
int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results;
   int pos = 0;
void setup()
{   

myservo.attach(9);
   irrecv.enableIRIn(); // Start the receiver
   Serial.begin(9600); // for serial monitor output
}
void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
    case 2148500501: Serial.write(" LEFT\n ");  for(pos = 90; pos <= 180; pos += 1)    { myservo.write(pos); }     break;
    case 2148467733: Serial.write(" LEFT\n");     for(pos = 90; pos <= 180; pos += 1)    { myservo.write(pos); }      break;
    case 2148467732: Serial.write(" RIGHT\n ");     for(pos = 90; pos >= 0; pos -= 1)    { myservo.write(pos); }     break;
    case 2148500500: Serial.write(" RIGHT\n");   for(pos = 90; pos >= 0; pos -= 1)    { myservo.write(pos); }    break;
}
}

void loop()

  if (irrecv.decode(&results)) // have we received an IR signal?
  {    Serial.write(" TEST\n ");
    translateIR();
    for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat
    {
      irrecv.resume(); // receive the next value
    }
  }
}

Jantje

GSoon
The code below will not "correct" but it should give you an understanding of what is wrong in your code.
Best regards
Jantje

Code: [Select]
#include <Servo.h>
#include <IRremote.h> // use the library
Servo myservo;
int receiver = 11; // pin 1 of IR receiver to Arduino digital pin 11
IRrecv irrecv(receiver); // create instance of 'irrecv'
decode_results results;
   int pos = 0;
void setup()
{   

myservo.attach(9);
   irrecv.enableIRIn(); // Start the receiver
   Serial.begin(9600); // for serial monitor output
}
void translateIR() // takes action based on IR code received
{
  switch(results.value)
  {
    case 2148500501:
    case 2148467733: Serial.write(" LEFT\n");    myservo.write(++pos);      break;
    case 2148467732:
    case 2148500500: Serial.write(" RIGHT\n");     myservo.write(--pos);      break;
}
}

void loop()
{
  if (irrecv.decode(&results)) // have we received an IR signal?
  {    Serial.write(" TEST\n ");
    translateIR();
    for (int z=0; z<2; z++) // ignore 2nd and 3rd signal repeat
    {
      irrecv.resume(); // receive the next value
    }
  }
}
Do not PM me a question unless you are prepared to pay for consultancy.
Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy