ir trigger state change

hi so i have got the first 2 parts of my project working correctly

now im trying to add manual control using ir remote (have the code already)

i have been researching and i think i need to change my code to a FSM to implement this.

can i use the ir remote to issue a state change or is it better to use aa toggle switch and pull up resister
to trigger the state change

Without seeing your code your question does not make sense.

The IR cannot "issue" a state change. However if the Arduino receives data from the IR device it can decide to change state based on that data.

Whether you use IR data or a toggle switch is something you must choose.

...R

hi thanks for your guidance once again here is my original code from previous thread

//========from lasermover======\\
#include <Servo.h>
Servo vert;
Servo hor;
//==========adding lcd to project=========\\
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // initialize the library with the numbers of the interface pins



void setup()
//=======laser mover======\\
{
  vert.attach(8);//vertical servo data pin
  hor.attach(9); //horizontal servo data pin
}

void migrate(Servo &myServo, int newPos) {
  int wait = random(30, 60); //randomize the wait to make it more interesting
  int pos = myServo.read(); //Read the current servo position
  if (pos < newPos) {
    for (int i = pos; i < newPos; i++) {
      myServo.write(i);
      delay(wait);
    }
  } else {
    for (int i = pos; i > newPos; i--) {
      myServo.write(i);
      delay(wait);
    }
  }
}


void randomPosition() 
{
  {
    int rand = random(40, 120); //The range is limited to 60 deg for better action
    migrate(hor, rand);

    rand = random(90, 135); //The vertical range is limited to 45 deg also for better action.
    migrate(vert, rand);
  }

  //========lcd setup========\\

  {
    lcd.begin(16, 2); // define lcd size
    lcd.print("vert:"); //static variable line 1 on lcd
    lcd.setCursor(0, 1); //Move to second line
    lcd.print("hor:");  //static variable line 2 on lcd
    int VerticalPosition = vert.read();
    int HorizontalPosition = hor.read();
    lcd.setCursor(6, 0); // set vert servo position lcd print location
    lcd.print(VerticalPosition);
    lcd.setCursor(6, 1); // set hor servo position lcd print location
    lcd.print(HorizontalPosition);
  }
}

void loop()
{
  servoLoopCode();
  lcdLoopCode();
}
void servoLoopCode()
{
  randomPosition();
  delay(2000);
}
void lcdLoopCode()
{
  int VerticalPosition = vert.read();
  int HorizontalPosition = hor.read();
  lcd.setCursor(6, 0); // set vert servo position lcd print location
  lcd.print(VerticalPosition);
  lcd.print(" ");
  lcd.setCursor(6, 1); // set hor servo position lcd print location
  lcd.print(HorizontalPosition);
  lcd.print(" ");
}

now i need to implement a state change to add this next part to my code

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

const byte RECV_PIN = 10;

IRrecv irrecv(RECV_PIN);
Servo vert;
Servo hor;

byte hposition = 90; // range 0-180
byte vposition = 90; // range 0-180
boolean newIRdata = false;
unsigned long IRinput;

decode_results results;

void setup()
{
  Serial.begin(9600);
  vert.attach(9);
  hor.attach(8);
  irrecv.enableIRIn(); // Start the receiver
}

void loop()
{
  if (irrecv.decode(&results))
  {
    IRinput = results.value;
    irrecv.resume(); // Receive the next value
    newIRdata = true;
  }
  if(newIRdata == true)
  {
   Serial.println(IRinput, HEX);
      switch(IRinput)
    {
     // move right 1 degree per button push
     case(0xFFC23D): // right arrow
     hposition-= 5;
     Serial.println(hposition);
     break;
     // move left 1 degree per button push
     case(0xFF22DD): // left arrow
     hposition+= 5;
     Serial.println(hposition);
     break; 
      // move right 1 degree per button push
     case(0xFF629D): // up arrow
     vposition-= 5;
     Serial.println(vposition);
     break;
     // move left 1 degree per button push
     case(0xFFA857): // down arrow
     vposition+= 5;
     Serial.println(vposition);
     break; 
      // center both servos
     case(0xFF02FD): //ok button
     vposition=90;
     hposition=90;
     Serial.println(vposition);
     Serial.println(hposition);
     break; 
    }
     IRinput = 0;
     newIRdata = false;   
  }
  hor.write(hposition);
  vert.write(vposition);
}

im wondering if i can trigger the state change with the ir remote or if i should add a toggle switch and use a high low value to trigger the states

You seem to have posted two completely different programs so I can’t figure what you are trying to do.

I still think you may be confusing yourself by the way you have framed your question

im wondering if i can trigger the state change with the ir remote

If you have a function that can read the IR data your program can decide what to do. Think of a program that is structured like this

void loop()
{
  receiveIRData();
  analyzeIRData();
  servoLoopCode();
  lcdLoopCode();
}

…R

yes they are 2 different codes one is going to move automatically and then I need to have a manual over ride so I need a state change. I was playing around with the it remote today and I can't figure out how to have it act like a push button to change states.

c0ryp1:
I can't figure out how to have it act like a push button to change states.

Can you write a short program to receive values from the IR device and print the values on the serial monitor?
If you can, post the code and I will try to show how to integrate it. Keep the code as short as possible with no unnecessary parts.

...R