hi i cant get a state change to work
//========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
//=========adding ir movements=========\\
#include <IRremote.h>
const byte RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
byte hposition = 90; // range 0-180
byte vposition = 90; // range 0-180
boolean newIRdata = false;
unsigned long IRinput;
decode_results results;
//====== adding manual override======\\
int buttonPin = 7;
int buttonState;
int S_automatic;
int S_manual;
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);
}
//======ir setup=====\\
{
pinMode(buttonPin, INPUT); // set pin as input
Serial.begin(9600); //starts the serial monitor
irrecv.enableIRIn(); // Start the receiver
}
}
void loop()
{
toggle();
servoLoopCode();
lcdLoopCode();
manualOverride();
}
void toggle()
{
manualOverride();
if(buttonPin = HIGH);
else;
servoLoopCode();
}
void servoLoopCode()
{
randomPosition();
delay(2000);
}
void manualOverride()
{
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);
}
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(" ");
}
i even tried to set up a fsm and it still will not override to manual
//========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
//=========adding ir movements=========\\
#include <IRremote.h>
const byte RECV_PIN = 10;
IRrecv irrecv(RECV_PIN);
byte hposition = 90; // range 0-180
byte vposition = 90; // range 0-180
boolean newIRdata = false;
unsigned long IRinput;
decode_results results;
//====== adding manual override======\\
int buttonPin = 13;
int buttonState;
int S_movement;
int S_manual;
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);
}
//======ir setup=====\\
{
pinMode(buttonPin, INPUT); // set pin as input
Serial.begin(9600); //starts the serial monitor
irrecv.enableIRIn(); // Start the receiver
}
}
void loop()
{
static int state = S_movement; // initial state is 1, the "auto" state.
switch(state);
{
case S_movement:
randomPosition();
delay(2000);
case S_manual
if (buttonState=HIGH)
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);
}
{
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(" ");
}
im almost to the point of just using 2 arduinos with a 3 position switch to toggle power with sharing data lines but i would rather not