servo sweep controlled by the ir remote with an LED

i have gotten to where i can turn the led on and off and it starts the servo sweep however the servo does not continue to sweep it just goes from position 0 to 1 and stops until i turn the led off and then back on here is the code any help would be greatly appreciated
thanks
wayne

#include <IRremote.h>
#include <Servo.h>

int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
int led1 = 10;
int itsONled = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1’s if you want a certain
led to light when the board is powered */
#define code1 0xFFA25D // code received from button A

Servo myservo;

int pos = 0;

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup()
{
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
myservo.attach(9);

}

void loop() {
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case code1:
if(itsONled[1] == 1) { // if first led is on then
digitalWrite(led1, LOW); // turn it off when button is pressed
itsONled[1] = 0; // and set its state as off
} else { // else if first led is off
digitalWrite(led1, HIGH); // turn it on when the button is pressed
itsONled[1] = 1; // and set its state as on
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15); // waits 15ms for the servo to reach the position
}
for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable ‘pos’
delay(15);
}
}
Serial.println(value); // you can comment this line
irrecv.resume(); // Receive the next value
}
}

The layout of your code is in a right mess. It has taken me a long time to put some shape on it. But I don’t know if it reflects what you want so please check it carefully.

#include <IRremote.h>
#include <Servo.h>
 
int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
int led1 = 10;
int itsONled[] = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1's if you want a certain
led to light when the board is powered */
#define code1  0xFFA25D // code received from button A

Servo myservo;

int pos = 0;
 

IRrecv irrecv(RECV_PIN);
 
decode_results results;
 
void setup() {
    Serial.begin(9600);   // you can comment this line
    irrecv.enableIRIn();  // Start the receiver
    pinMode(led1, OUTPUT);
    myservo.attach(9);
}
 
void loop() {
    if (irrecv.decode(&results)) {
        unsigned int value = results.value;
        switch(value) {
            case code1:
                if(itsONled[1] == 1) {
                    digitalWrite(led1, LOW);
                    itsONled[1] = 0;
                 }
                 else { 
                    digitalWrite(led1, HIGH);
                    itsONled[1] = 1;
                    for(pos = 0; pos <= 180; pos += 1) { 
                        myservo.write(pos);
                        delay(15); 
                    }
                    for(pos = 180; pos>=0; pos-=1) {           
                        myservo.write(pos);
                        delay(15);
                    }
                }
        }
        Serial.println(value);
        irrecv.resume(); 
    }
}

I won’t make any attempt to review the code until you say whether it now reflects your intentions.

Having a single SWITCH CASE is pointless but I have left it in on the assumption that you intended to add more CASEs.

…R

so your code worked to an extent i pushed the button on my ir remote and the servo did one full sweep and then stopped i need it to continuously keep sweeping. i am creating a putt putt game in which you would putt the ball past clown teeth that are moving up and down with the servo. Basically the code is correct for the teeth to go up and down once but then stop. How do i get it to keep going.
Thanks
Wayne

i need it to continuously keep sweeping.

Maybe put the basic sweep code in a looping sub routine that once entered, it is never exited. Is that really what you want?

waynebuhler117:
so your code worked to an extent

Did you carefully study the changes I made and compare them to your original code with particular emphasis on whether my changes are consistent with the logic you had in mind originally. I may easily have misunderstood things.

Another way forward would be write down what you want to happen as a series of steps like this (entirely fictitious example)

press buttonX on remote
ledA turns on
wait 5 secs
ledA goes off
servo sweeps o to 180
etc

Then I will be able to relate those desired steps to your code.

...R

ok so this is what i would ultimately want

press button 1 on remote
LED turns on and servo begins sweep (0 to 180 to 0 to 180) continuously going
press button 1 on remote again LED turns off and servo stops
Press button 2 on remote
LED turns on and servo begins sweep this time with a shorter delay making servo "move" faster
press button 2 on remote LED turns off along with the servo sweep
Thanks for all the help
Wayne

waynebuhler117:
ok so this is what i would ultimately want

Thanks. That seems clear. I will have a look at it in the morning.

...R

You need to keep track of the state of your system, perhaps with a couple of variables called button1pressed and button2pressed

When the remote is pushed it toggles the appropriate button

The control of the servos and LEDs depends on the variables rather than directly on the buttons.

And you MUST move the servos one step at a time, and not with
for(pos = 0; pos <= 180; pos += 1)
so that the new value of the variables can be checked while the servo is moving

The general style of the code could be like this pseudo code

void loop() {
   readButtons();
   moveServos();
   updateLEDs();
}

void readButtons() {
    // check IR input and update state variables
}

void moveServos() {
   if (button1pressed == true) {
       // move servo in step
       // if servo is at end of range reverse direction or stop
   }
  else {
      // whetver should happen ....
  }
}

The code in the demo several things at a time probably has all the necessary parts

…R

sorry you completely lost me on that one instead of using for(pos = 0; pos <= 180; pos += 1) you want me to use the pseudo code you wrote there?
Thanks
Wayne

waynebuhler117:
sorry you completely lost me on that one instead of using for(pos = 0; pos <= 180; pos += 1)

What I was trying to say is that the FOR loop goes through all 180 steps in one process. What you need to do is just move one step at a time in each iteration of loop() and use a variable to keep track of the angle that has been reached so you don’t go beyond 180.

Something like this

void loop() {
    servoPos += 1;
    if (servoPos > 180) {
        servoPos = 180;
    }
    moveServo();
}

void moveServo() {
   myServo.write(servoPos);
}

The pseudo code is intended to give an impression of how the entire program should be structured. It is the same general style that is used in several things at a time and in planning and implementing a program.

…R

ok so i have figured out my code for the most part i push the power button my LED powers on and the servo begins to sweep and does not stop now i just need to figure out how to turn everything back off. Can i use the same button to power off or should i press a different button to make everything stop

heres my code

#include <IRremote.h>
#include <Servo.h>

int RECV_PIN = 11; // the pin where you connect the output pin of TSOP4838
int led1 = 10;
int itsONled = {0,0,0,0};
/* the initial state of LEDs is OFF (zero)
the first zero must remain zero but you can
change the others to 1’s if you want a certain
led to light when the board is powered */
#define button1 0xFFA25D // code received from button A

Servo myservo;

int pos = 0;

#define button1pressed 0xFFa25D

IRrecv irrecv(RECV_PIN);

decode_results results;

void setup() {
Serial.begin(9600); // you can comment this line
irrecv.enableIRIn(); // Start the receiver
pinMode(led1, OUTPUT);
myservo.attach(9);
}

void loop() {
if (button1pressed)
if (irrecv.decode(&results)) {
unsigned int value = results.value;
switch(value) {
case button1:
if (itsONled[1] == 1) {
digitalWrite(led1, LOW);
itsONled[1] = 0;
}
else {
digitalWrite(led1, HIGH);
itsONled[1] = 1;

while(itsONled[1] == 1)
{
for(pos = 0; pos < 180; pos += 1) {
myservo.write(pos);
delay(15);
}
for(pos = 180; pos>=1; pos-=1) {
myservo.write(pos);
delay(15);
}
}}
}
Serial.println(value);
irrecv.resume();
}
}

Please make it easier to help you by post your code in code tags.

This line

 if (button1pressed)

makes no sense. Button1pressed seems to be some strange number that was set at the top of the program and nowhere else.

I thought the idea was that receiving something specific from the remote control would determine whether the button had been pressed. Perhaps something like

remoteValue = results.value;
if (remoteValue == XXXXX) {
   buttonPressed = true;
}
else {
   buttonPressed = false;
}

And you still have the completely superfluous CASE statement.

What sort of things do you get in results.value? Does it produce a character string, or an int or ?

...R