Using 'Arm' Button.. SO CLOSE! Help please!!

The code works as it should, but the other day I was asked to add an arm button so that the code doesn’t execute UNLESS the arm button is pressed, and THEN once it runs the loop, the arm button goes back to a low state and doesn’t re-execute unless re-armed. The code I have is:

const int redled = 2;
const int greenled = 7;
const int rotstop = 3;
const int rotwalk = 4;
const int rotrun = 6;
const int armbutton = 12;
const int armled = 13;
int armstate = 0;
int lastarmstate = 0;
int arm = 0;
int incomingByte;
int rotstopstate = 0;
int rotwalkstate = 0;
int rotrunstate = 0;
int buttonpushcounter = 0;

#include <Servo.h>

Servo esc;
void setup() {

Serial.begin(9600);
pinMode(redled, OUTPUT);
pinMode(greenled, OUTPUT);
pinMode(rotstop, INPUT);
pinMode(rotwalk, INPUT);
pinMode(rotrun, INPUT);
esc.attach(9);
pinMode(armled, OUTPUT);
}

void loop() {

armstate = digitalRead(armbutton);
rotstopstate = digitalRead(rotstop);
rotwalkstate = digitalRead(rotwalk);
rotrunstate = digitalRead(rotrun);

/////// WALK ///////

else if (rotwalkstate == HIGH)
{
if (armstate != lastarmstate)
{
arm = 1;
digitalWrite (armled, HIGH);
}
if (arm == 1)
{
buttonpushcounter++;
if (Serial.available() > 0)
{
incomingByte = Serial.read();
if (incomingByte == ‘H’)
{
esc.write(140);
digitalWrite(greenled, HIGH);
digitalWrite(redled, LOW);
delay(4000);
}
else if (incomingByte == ‘L’)
{
digitalWrite(redled, HIGH);
digitalWrite(greenled, LOW);
esc.write(100);
arm = 0;
digitalWrite(armled, LOW);
}
}
}
}

I’m being triggered off of a sensor on another UNO via Zigbee. The problem I’m having is, it works IF I’m pressing (and holding) the button, but as soon as I let go, the code stops. What I want is, once the UNO see’s the Arm Button’s high state, it will enter into an ‘Arm’ state and waits until the incomingByte is ‘H’, and then it runs the code and once it see’s the incomingByte change to ‘L’, it then disarms and waits for the re-arm. I understand that I’m getting ‘L’ on my incomingByte BEFORE the ‘H’, and that is probably why I have to hold the Arm Button or else it dis-arms. But how do I then change that? Thanks in advance everyone!!

ALSO, I left out some code that isn't super relevant I dont think, which is why it starts with an 'else if' statement. Thanks!

Something like this ? (not complete, not tested)

void loop() {
   buttonState = digitalRead(buttonPIn);
   if (buttonState == LOW && prevButton == HIGH) {
       runCode = true;
       prevButton = buttonState;
   }
   if (runCode == true) {
      runCode = false;
      myFunction();
   }
}

void myFunction () {
  // principal code here
}

Edited to change myFuntion to myFunction

...R

I hate to ask you to do this, as I understand you are very active/busy on this forum, but would you mind writing a little more detail in the code you posted? I am another rookie on this, and am following, just not sure which of those I should declare integers, and which of those I should declare constant integers. Also, what you're calling 'buttonstate', is that supposed to be my 'armstate'? Same question with your 'runCod = true;' supposed to be my 'arm = 1'?

vette505: but would you mind writing a little more detail in the code you posted? I am another rookie on this,

I would like to see you learn from this exercise so you should make the first attempt at the code based on my suggestion. Then try your code and post it together with info about how it performs and I will certainly help at that stage.

...R

Robin2: I would like to see you learn from this exercise so you should make the first attempt at the code based on my suggestion. Then try your code and post it together with info about how it performs and I will certainly help at that stage.

...R

Fair enough. One question then, because I have already tried your code. I'm just getting hung up on 'void myFunction'. Not sure what this is, nor does my computer. Thanks!

vette505: Fair enough. One question then, because I have already tried your code. I'm just getting hung up on 'void myFunction'. Not sure what this is, nor does my computer. Thanks!

void myFunction is a function definition. You can define a function that can be called from your main loop. You can see where Robin2 calls it in the "if (runCode == true)" statement. (Actually, he calls the myFunction declaration, but declares it as myFuntion, which I'm sure would cause myself lots of headaches as I spent an hour looking for a mis-spelling. :))

I see! Well I'm going to mess with it more and I will keep you guys posted with home I'm doing. I'm hanging it up until Monday, but I will hit it hard again then. Thanks for the everything!!

iyogurt: (Actually, he calls the myFunction declaration, but declares it as myFuntion,

Thanks for spotting that. I have corrected it.

...R

vette505: because I have already tried your code. I'm just getting hung up on 'void myFunction'. Not sure what this is, nor does my computer. Thanks!

It is virtually impossible to help if you don't post your code so that we can see what you can see.

...R

Hi,

Can you please post a copy of your sketch, using code tags? See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Tom.... :)

Hey guys, after a long weekend I’m back at it! So the code I tried is:

const int redled = 2;
const int greenled = 7; 
const int rotstop = 3;
const int rotwalk = 4;
const int rotrun = 6;
const int armbutton = 12;
const int armled = 13;
int armstate = 0;
int lastarmstate = 0;
int arm = 0;
int incomingByte;
int rotstopstate = 0;
int rotwalkstate = 0;
int rotrunstate = 0;
int buttonpushcounter = 0;


#include <Servo.h>

Servo esc;
void setup() {
 
 Serial.begin(9600);
 pinMode(redled, OUTPUT);   
 pinMode(greenled, OUTPUT); 
 pinMode(rotstop, INPUT);
 pinMode(rotwalk, INPUT);
 pinMode(rotrun, INPUT); 
 esc.attach(9);
 pinMode(armled, OUTPUT);
}

void loop() {
 
 armstate = digitalRead(armbutton);
 rotstopstate = digitalRead(rotstop);
 rotwalkstate = digitalRead(rotwalk);
 rotrunstate = digitalRead(rotrun);
   
 if (armstate == LOW && lastarmstate == HIGH)
 {
   arm = 1;
   lastarmstate = armstate;
 }
 if (arm == 1)
 {
   arm = 0;
   myFunction ();
 }
 
 void myFunction ()
 {
   if (rotstopstate == HIGH)
 {    
   if (Serial.available() > 0) 
   {
     incomingByte = Serial.read();   
     if (incomingByte == 'H')
     { 
       esc.write(60);
       digitalWrite(greenled, LOW);
       digitalWrite(redled, LOW);
       delay(4000);
     }
     else if (incomingByte == 'L')
     {
       digitalWrite(redled, HIGH);
       digitalWrite(greenled, LOW);
       esc.write(100);
     }     
   }
 }

Am I even close? Thanks!

vette505: Am I even close? Thanks!

Does your program compile? Does it work the way you want it to?

If the answer to either is NO then tell us exactly what happens.

And PLEASE modify your Reply #11 to put the code between code tags so it is easy to read and to select and copy to a text editor.

...R

Neither loop nor myfunction have a closing brace. Add them and it will compile at least.

Hi, Count your { and } There are 3 } missing, that is where the error is pointing.

Tom..... :)

Thanks Bill! I will do that now! Will update in a minute or two..!

And thank you Tom as well! So this is the complete program I currently have, which does compile now. However, the Arduino is unresponsive to the state change from my arm button. Nothing happens…no matter what I do. I’m honestly stumped on this. This is the first time I’ve ever used Arduino, and I’ve had no teaching on this ever. I will keep plugging away though! Thanks guys!

const int redled = 2;
const int greenled = 7; 
const int rotstop = 3;
const int rotwalk = 4;
const int rotrun = 6;
const int armbutton = 12;
const int armled = 13;
int armstate = 0;
int lastarmstate = 0;
int arm = 0;
int incomingByte;
int rotstopstate = 0;
int rotwalkstate = 0;
int rotrunstate = 0;
int buttonpushcounter = 0;


#include <Servo.h>

Servo esc;
void setup() {
  
  Serial.begin(9600);
  pinMode(redled, OUTPUT);   
  pinMode(greenled, OUTPUT); 
  pinMode(rotstop, INPUT);
  pinMode(rotwalk, INPUT);
  pinMode(rotrun, INPUT); 
  esc.attach(9);
  pinMode(armled, OUTPUT);
}

void loop() {
  
  armstate = digitalRead(armbutton);
  rotstopstate = digitalRead(rotstop);
  rotwalkstate = digitalRead(rotwalk);
  rotrunstate = digitalRead(rotrun);
    
  if (armstate == LOW && lastarmstate == HIGH)
  {
    arm = 1;
    lastarmstate = armstate;
  }
  if (arm == 1)
  {
    arm = 0;
    myFunction ();
  }
}
  
void myFunction ()
{
    /////// STOP ///////
    
  if (rotstopstate == HIGH)
  {    
    if (Serial.available() > 0) 
    {
      incomingByte = Serial.read();   
      if (incomingByte == 'H')
      { 
        esc.write(60);
        digitalWrite(greenled, LOW);
        digitalWrite(redled, LOW);
        delay(4000);
      }
      else if (incomingByte == 'L')
      {
        digitalWrite(redled, HIGH);
        digitalWrite(greenled, LOW);
        esc.write(100);
      }     
    }
  }
    
    /////// WALK ///////
    
  else if (rotwalkstate == HIGH)
  {    
    if (Serial.available() > 0) 
    {
      incomingByte = Serial.read();   
      if (incomingByte == 'H')
      { 
        esc.write(140);
        digitalWrite(greenled, HIGH);
        digitalWrite(redled, LOW);
        delay(4000);
      }
      else if (incomingByte == 'L')
      {
        digitalWrite(redled, HIGH);
        digitalWrite(greenled, LOW);
        esc.write(100);
      }     
    }
  }
    
    /////// RUN ///////
    
  else if (rotrunstate == HIGH)
  {    
    if (Serial.available() > 0) 
    {
      incomingByte = Serial.read();   
      if (incomingByte == 'H')
      { 
        esc.write(180);
        digitalWrite(greenled, HIGH);
        digitalWrite(redled, LOW);
        delay(4000);
      }
      else if (incomingByte == 'L')
      {
        digitalWrite(redled, HIGH);
        digitalWrite(greenled, LOW);
        esc.write(100);
      }     
    }
  }
  
  else
  {
    //digitalWrite(redled, HIGH);
    esc.write(100);
  } 
}

Robin2: Does your program compile? Does it work the way you want it to?

If the answer to either is NO then tell us exactly what happens.

And PLEASE modify your Reply #11 to put the code between code tags so it is easy to read and to select and copy to a text editor.

...R

Hey Robin. It does not compile. It says error and does not like the "myFuntion" statement. I just copy and pasted my entire code. Did I write it as you thought I should? Thanks!

The last code you posted compiles for me. Perhaps you had better post the most recent problem code.

wildbill: The last code you posted compiles for me. Perhaps you had better post the most recent problem code.

Hey Bill. It compiles for me too, but nothing happens. When I press arm, there is no output, the LED's don't light up. Nothing. Just doesnt respond. I uploaded again to make sure there weren't any hicups..but still nothing.