Pages: [1]   Go Down
Author Topic: Coding Error in My Sketch - Any Ideas?  (Read 377 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
    }
  }
}
Logged

Belgium
Offline Offline
Edison Member
*
Karma: 68
Posts: 1903
Arduino rocks; but with my plugin it can fly rocking the world ;-)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
    }
  }
}
Logged

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 -

Pages: [1]   Go Up
Jump to: