How To STOP Servo Motor Sweep Using IRremote

Hello Forum Member,
I am trying to learn how to control servo motor using irremote.
first code below working as per expected.

#include <IRremote.h>
#include <Servo.h>
int IRpin = 4;
String kodeKu;

struct kode
{
  uint32_t kodeValue;
  const char * ku;
} KodeList[] =
{
  {0xE916FF00, "zero"},
  {0xE619FF00, "RPT"},
  {0xF20DFF00, "U/SD"},
  {0xF30CFF00, "one"},
  {0xA15EFF00, "three"},
};

const char * GetKodeKu(uint32_t kode)
{
  size_t kuCount = sizeof KodeList / sizeof KodeList[0];
  for (size_t i = 0; i < kuCount; i++)
  {
    if (KodeList[i].kodeValue == kode)
    {
      Serial.print("Recognized as ");
      Serial.println(KodeList[i].ku);

      return KodeList[i].ku;
    }
  }
  Serial.println("Kode Not Recognized");
  return "";
}
Servo servo1;
const int SERVO1_PIN = 7;
int servo1_angle = 90;
int servo1_angleStep = 1;
const int servo1_ANGLE_CENTRE = 90;
bool button_servo1State_on , button_servo1State_off;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(200);
  Serial.println("TEST OF RCSERVO CONTROL USING IRREMOTE");
  IrReceiver.begin(IRpin);
  servo1.attach(SERVO1_PIN);
  IrReceiver.enableIRIn();
}

void loop() {
  // put your main code here, to run repeatedly:
  while (IrReceiver.decode() == 0);
  unsigned long value = (IrReceiver.decodedIRData.decodedRawData);
  Serial.print("Received: ");
  IrReceiver.resume();
  kodeKu = GetKodeKu(value);

  switch (value) {
    case 0xE916FF00 :
      Serial.println("zero");
      servo1.write(servo1_ANGLE_CENTRE);
      Serial.println(servo1_ANGLE_CENTRE);
      delay(1000);
      break;
    case 0xE619FF00:
      Serial.println("RPT");
      if (servo1_angle > 50 && servo1_angle <= 130) {
        servo1_angle = servo1_angle - servo1_angleStep;
        if (servo1_angle <= 50) {
          servo1_angle = servo1_angle;
        } else {
          servo1.write(servo1_angle); // move the servo to desired angle
          Serial.print("Moved to: ");
          Serial.print(servo1_angle);   // print the angle
          Serial.println(" degree");
        }
      }
      delay(100);
      break;
    case 0xF20DFF00:
      Serial.println("U/SD");
      if (servo1_angle >= 50 && servo1_angle <= 130) {
        servo1_angle = servo1_angle + 1;
        if (servo1_angle >= 130) {
          servo1_angle = servo1_angle;
        } else {
          servo1.write(servo1_angle); // move the servo to desired angle
          Serial.print("Moved to: ");
          Serial.print(servo1_angle);   // print the angle
          Serial.println(" degree");
        }
      }
      delay(100);
      break;

then i want to control servo sweep using another irremote button, in this case using button "three". I put code as below:

    case 0xA15EFF00 :
      Serial.println("three");
      button_servo1State_off = !button_servo1State_off;
      button_servo1State_on = false ;
      while (button_servo1State_off = true) {
        for (servo1_angle = 50; servo1_angle <= 130; servo1_angle++) { // goes from 50 degrees to 130 degrees
          // in steps of 1 degree
          servo1.write(servo1_angle); // tell servo to go to position in variable 'pos'
          Serial.println(servo1_angle);
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        for (servo1_angle = 130; servo1_angle >= 50; servo1_angle-- ) { // goes from 130 degrees to 50 degrees
          servo1.write(servo1_angle);              // tell servo to go to position in variable 'pos'
          Serial.println(servo1_angle);
          delay(15);  // waits 15 ms for the servo to reach the position
        }
      }
      break;

it was working and servo start sweep.
after this i have no clue how to stop servo sweep at any angle and position using button "one" then return to normal condition before start any irremote button.
i put the code below but doesn't response. what should i do next? please guide me. thank you

       case 0xF30CFF00 :
      Serial.println("one");
      button_servo1State_off = !button_servo1State_off;
      button_servo1State_on = false ;
      while (button_servo1State_off = true) {
        if("play" == false) break;
        for (servo1_angle = 50; servo1_angle <= 130; servo1_angle+0) { // goes from 0 degrees to 180 degrees
          // in steps of 1 degree
          servo1.write(servo1_angle); // tell servo to go to position in variable 'pos'
          Serial.println(servo1_angle);
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        for (servo1_angle = 130; servo1_angle >= 50; servo1_angle-0 ) { // goes from 180 degrees to 0 degrees
          servo1.write(servo1_angle);              // tell servo to go to position in variable 'pos'
          Serial.println(servo1_angle);
          delay(15);  // waits 15 ms for the servo to reach the position
        }
      }
      break;
  }
}

Your challenge is once you have pressed '3' your code is stuck in this infinite while loop as nothing changes the state of button_servo1State_off

      while (button_servo1State_off = true) {
        for (servo1_angle = 50; servo1_angle <= 130; servo1_angle++) { // goes from 50 degrees to 130 degrees
          // in steps of 1 degree
          servo1.write(servo1_angle); // tell servo to go to position in variable 'pos'
          Serial.println(servo1_angle);
          delay(15);                       // waits 15 ms for the servo to reach the position
        }
        for (servo1_angle = 130; servo1_angle >= 50; servo1_angle-- ) { // goes from 130 degrees to 50 degrees
          servo1.write(servo1_angle);              // tell servo to go to position in variable 'pos'
          Serial.println(servo1_angle);
          delay(15);  // waits 15 ms for the servo to reach the position
        }
      }

so you don't listen to the remote anymore

you need to read tutorials about millis() and doing multiple things at the same time

PS: the other challenge is of course the ooooops on
while (button_servo1State_off = true)
but that's an easily fixed typo

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.