please help cant get this to work

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

The "delay(2000)" will stop the sketch for 2 seconds. Nothing can be done during those 2 seconds.

To detect a button, the loop() has to run without the use of such a delay. That can be achieved with millis().
The big advantage is that millis() goes well with a state machine.
You would have to rewrite parts of the sketch and change the way you use the loop with the servo commands and that delay.

Could you use brackets '{' and '}' and indents always in the same way ? In the menu is a auto-text-format tool that you can use as a start.
I think it is allowed to use '{' and '}' for blocks of code, but they are never used that way.

Your code:

{
  // block one
  ...
}

{
  // block two
  ...
}

The comments can be used to show that it is a seperate block of code, the '{' and '}' can be omitted.

I don't see a working state machine in the second sketch, perhaps you mean something different with a fsm ?

That's what I was going to post as well.

Here is a very useful resource:

Also, you should always initialize your variables. Not sure on how arduino interprets it but it's one of the golden rules of C++. You can initialize directly when you declare it or in the setup loop. But doing something like "static int status = s_movement;" when you didn't initialize s_movement is hazardous. (and you forgot a break; after your first case and a double point after your second case.

In most cases, C++ guarantees that variables are initialized to zero at run time.

Have a look at the code in Several Things at a Time and in Planning and Implementing a Program. Both use millis() instead of delay() so that buttons etc can be detected immediately.

...R

  if(buttonPin = HIGH);
  else;

It is unusual to assign a value to a variable in an if statement. You probably meant to use ==, instead of =.

But that would make your code, using curly braces:

  if(buttonPin == HIGH)
  {
     ;
  }
  else
  {
     ;
  }

which means "If the switch is pressed, do nothing. Otherwise, do nothing." Which is a waste of time.

thanks I'll see if I can understand millis if the == does not work

I can see you have weaned yourself from my advises little too soon.
That's OK, but you are making few mistakes by flying little too fast and high.
Check you C code syntax first, compiler is helpful but cannot check / catch everything.

Here is a list I can see without being able to compile your code since I do not have required libraries and see no point to bypass their code for now anyway:

if(true);
else;

switch(variable);

case VariableX

PS Tools -> Auto Format helps in verifying the brackets too.

yes i think i am going to fast for my self too i tried to clean up my code a little because i was getting lost while trying to add new sections and now the same thing is happening while i try to add manual override

any ways heres what i have come up with reading about milis

//========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;
 #define S_IDLE 1
 #define S_movement 2
 #define S_manual 3

//====== milis=====\\
unsigned long interval=2000;  // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

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:
   if (buttonState == HIGH)
   randomPosition();
   if ((unsigned long)(millis() - previousMillis) >= interval)

 case S_manual 
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(" ");
}

but i am getting a error "case s_manual not within switch statement" during verify
but is that not what switch is for?

im not sure what you mean by
if(true);
else;
switch(varaiable)

You cant put this line where it is

   if ((unsigned long)(millis() - previousMillis) >= interval)

unless it has { } and content to happen if the interval is exceeded.

Have you studied the links in Reply #5 carefully, and tried the code?

And every case should end with break;

...R

c0ryp1:
... i am getting a error "case s_manual not within switch statement" during verify
but is that not what switch is for? ...

Punctuation --> : <--

but i am getting a error "case s_manual not within switch statement" during verify
but is that not what switch is for?

 switch (state)

  case S_movement:

The code block for the switch statement needs to be enclosed by { and }

//=======laser mover======\\

Don't do this shit. The \ has special meaning.

You also need to get in the habit of CONSISTENTLY placing curly braces.

void setup()
{
void migrate(Servo &myServo, int newPos) {

are not consistent. I prefer the first style, since the closing brace lines up with the opening one.

You also need to KNOW where { and } are needed or optional but preferred, and stop splattering others in the code unnecessarily.

void randomPosition() {
  {

The second {, and it's matching }, are not needed.

i think i am going to fast for my self too

You are setting the pace. Slow down. There are some studies that show that "listen, see, do, sleep, test" will produce higher test scores than "listen, see, do, test".

That is, learning a small amount, and then sleeping on it, makes it easier to remember and apply correctly, than does not sleeping for a while after the learning part.

Robin2:
You cant put this line where it is

   if ((unsigned long)(millis() - previousMillis) >= interval)

unless it has { } and content to happen if the interval is exceeded.
ssss
Have you studied the links in Reply #5 carefully, and tried the code?

And every case should end with break;

...R

i read the code for "blink without delay" and then took the line that triggered the millis and then added it to my code and figured it made sense to be there.....

i also read that every state should end with breaks; but then the verify said no breaks within the switch statements so i removed them

PaulS:

//=======laser mover======\\

Don't do this shit. The \ has special meaning.

You also need to get in the habit of CONSISTENTLY placing curly braces.

void setup()

{






void migrate(Servo &myServo, int newPos) {



are not consistent. I prefer the first style, since the closing brace lines up with the opening one.

You also need to KNOW where { and } are needed or optional but preferred, and stop splattering others in the code unnecessarily.


void randomPosition() {
  {



The second {, and it's matching }, are not needed.
You are setting the pace. Slow down. There are some studies that show that "listen, see, do, sleep, test" will produce higher test scores than "listen, see, do, test".

That is, learning a small amount, and then sleeping on it, makes it easier to remember and apply correctly, than does not sleeping for a while after the learning part.

didnt know \ was used any where it just made it easy to see sorry

and ive been adding extra braces because the verify keeps popping up a error expected {} befor/after ; or something and adding them was making it compile

....im a machinist by trade and the only experience in c++ i have was 6 years ago in grade 11 and 12 computer engineering and the teacher pretty much gave us broken codes and then had us fix it to work properly.
so i cannot yet write a entire code my self unless i read, find the statement that was doing what i want to take from the example,
and then cut and paste then utilize verify to make it work.

c0ryp1:
then added it to my code and figured it made sense to be there.....

I hope you now understand that it doesn't.

Using millis() is not as simple as single line of code, but when you understand the concept it is not complicated.

You need to think carefully about how the code in Several Things at a Time works before you try to implement it in your own project.

If there is something about that demo that you don't understand, please say what it is and we will try to help.

...R

didnt know \ was used any where it just made it easy to see sorry

It's a continuation character. It tells the compiler to ignore the carriage return. So
void setup()
// comment \
{
}
is seen by the compiler as

void setup()
// comment \\{
}

and, since the [ is commented out...

and ive been adding extra braces because the verify keeps popping up a error expected {} befor/after ; or something and adding them was making it compile

Not the right approach. Study the error messages. Learn where { and } should be.

the teacher pretty much gave us broken codes and then had us fix it to work properly.

That's very good practice for learning the syntax of a language.

so i cannot yet write a entire code my self

That will come.

read thru the example and im not seeing why this wont work

case S_movement:
  if (buttonState == HIGH)
    randomPosition();
      if ((unsigned long)(millis() - previousMillis) >= interval)
        break;

because i have defined the interval as 2000 and the previous as 0 in setup

unsigned long interval = 2000; // the time we need to wait
unsigned long previousMillis = 0; // millis() returns an unsigned long.
[code/]

its also spitting the break not within a loop out again? from what i understand break exits and returns to beginning of loop so it should check if button is activated and then keep running that section

PaulS:
Not the right approach. Study the error messages. Learn where { and } should be.

the braces open and close the sections of code that you want read during what it is trying to accomplish so it doesn't over calculate and overload the processor doesn't it?