Having Trouble combining two codes that work separate from each other

i am getting an error code for not defined, i am new at this. i quite figure out what i need to do. i am getting error for "timeSinceLastStateChange" both these programs work stand alone,

MEGAPROGRAM.ino (3.77 KB)

Easy: delete the curly brace on line 66.

I did it and it compiles perfectly.

Thanks Power, now after i did that and set up my boards there seems to be some lag for better words, when the right wire is pulled alot of the time both leds ledpin,and ledpin2 both light up when ledpin is supposed to stay lite and ledpin2 supposed to go out, and the 8 leds i have strobing lag way down, i am running a mega 2560 and the 8 leds are running thru a SN74HC595N

Spie303:
Thanks Power, now after i did that and set up my boards there seems to be some lag for better words, when the right wire is pulled alot of the time both leds ledpin,and ledpin2 both light up when ledpin is supposed to stay lite and ledpin2 supposed to go out, and the 8 leds i have strobing lag way down, i am running a mega 2560 and the 8 leds are running thru a SN74HC595N

Hold up, you didn't explain the project and how you expect it to work. Can you give more details on that pleas?

Project is an airsoft/paint ball prop. defusable timer. timer, 4 digit code, unhook one of the wires,some leds strobeing for distraction if timer zeros out or wrong wire is unhooked siren goes off

Here is what I found (tell me if this helps or not):

  1. When you change to STATE_defused (when the right wire is pulled I assume), you have the following code:
case STATE_defused:
    {
      // OUTPUTS:
      digitalWrite(ledpin2, HIGH);
      tone(8, 440);
      delay(3000);
      digitalWrite(ledpin2, LOW);
      noTone(8);
      // INPUTS:
      if (timeSinceChange >= 15000)
      { 
       setStateTo(STATE_unarmed);
      }
      break;
    }

There are three problems here:

  1. You do not make sure that ledpin is lit. Since the ledpin is blinking in the previous state, you might enter the defused state with the ledpin off and it will never turn on until returning to the armed state.

  2. You are blinking ledpin2 in the above code, not turning it off.

  3. When you use delay(3000), you are "blocking" the code and will prevent the MCU to process any other instructions. Basically, the Arduino will hang for 3 sec. This will cause about a 3 sec delay in all other features of your project (maybe why your strobe effect is laggy?). There is a way to blink without delay().

Does this help?

sorta ledpin is the red led that is the one letting you know that the alarm is active and ledpin2 green led is the all clear. so as far as the delay set it () and that should help the laggyness??

Spie303:
sorta ledpin is the red led that is the one letting you know that the alarm is active and ledpin2 green led is the all clear. so as far as the delay set it () and that should help the laggyness??

ok now that makes sense now that is working and when pull wrong wire is now where is stalls

case STATE_defused:
    {
      // OUTPUTS:
      digitalWrite(ledpin2, HIGH);
      tone(8, 440);
      delay(2);
      digitalWrite(ledpin, LOW);
      noTone(8);
      // INPUTS:
      if (timeSinceChange >= 15000)
      { 
       setStateTo(STATE_unarmed);
      }
      break;
    }

The code you quoted in your last post is for STATE_defused, not STATE_kaboom. Please post the updated STATE_kaboom code.

P.S. With what you've learned so far, you should be able to figure out what's wrong with it, but let me know if you still have trouble.

okay i changed my coding to get the timer stuff out and now i am having a exit status 1 case label "" not within a switch statement. i cant find where i went wrong do you mind glancing at it

int statled = 13;
int clearled = 12;
int badwire1 = 2;
int badwire2 = 3;
int rightwire = 4;
int badwire3 = 5;
int rightwire2 = 6;

int ledState = LOW;

enum State { STATE_armed, STATE_kaboom, STATE_defused } myState;

void setup() {

  Serial.println("begin");
pinMode(statled, OUTPUT);
pinMode(clearled, OUTPUT);
pinMode(rightwire, INPUT_PULLUP);
pinMode(rightwire2, INPUT_PULLUP);
pinMode(badwire1, INPUT_PULLUP);
pinMode(badwire2, INPUT_PULLUP);
pinMode(badwire3, INPUT_PULLUP);


}

void loop(){
 switch (myState);
   {
    case STATE_armed:
    {
   // OUTPUTS:
      digitalWrite(statled, HIGH);
      delay(10);
      digitalWrite(statled, LOW);
      delay(100);
      
      digitalWrite(clearled, LOW);
      delay(0);
      digitalWrite(clearled, LOW);
      delay(0);
   }
    //INPUTS:
    {
        Serial.println("Armed");
        }
    
    {
     if (digitalRead(badwire1) == HIGH || digitalRead(badwire2) == HIGH || digitalRead(badwire3) == HIGH)
               {
              Serial.println("Kaboom");
              setStateTo (STATE_kaboom);
               }
          else if (digitalRead(rightwire1) == HIGH || digitalRead(rightwire2) == HIGH)
               {
              Serial.println("Defused");
              setStateTo (STATE_defused);
               }
          
               {
              Serial.println("Kaboom");
              setStateTo (STATE_kaboom);
               }
          else 
               {
                 if (ledState == LOW)
                   { 
                    ledState = HIGH;
                  }
                  else
                   {
                    ledState = LOW;
                   }
                    statled = LOW;
                  }
                  digitalWrite(clearled, statled);
                }
               }

      break;
               }
    case STATE_kaboom:
    {
      // OUTPUTS:
      digitalWrite(statled, HIGH);
      delay(100);
      digitalWrite(stateled, LOW);
      delay(10);
      digitalWrite(clearled, HIGH);
      delay(10);
      digitalWrite(clearled, LOW);
      delay(100);
}
    case STATE_defused:
    {
      // OUTPUTS:
      digitalWrite(statled, HIGH);
      delay(0);
      digitalWrite(statled, LOW);
      delay(0);
      digitalWrite(clearled, LOW);
      delay(1);
      digitalWrite(clearled, LOW);
      delay(1);

      //INPUTS:
      setStateTo(STATE_unarmed);
      }
      break;
    }
    default:
    {
      setStateTo(STATE_unarmed);
      break;
    }
  }
}


void setStateTo(State new_state)
{
  myState = new_state;
}
 switch (myState);

This semi colon terminates the switch before any case statements.

Oh okay, I changed that and the code is all loaded with errors,

if you use the auto format tool, and match parentheses you will immediately find your problem... incorrect bracketing of your switch/case statements.

BulldogLowell:
if you use the auto format tool, and match parentheses you will immediately find your problem... incorrect bracketing of your switch/case statements.

thank you that was very helpful i am running in to case and break errors and i have read on them. and is not making any sense to me, the case's are right

Spie303:
thank you that was very helpful i am running in to case and break errors and i have read on them. and is not making any sense to me, the case's are right

Well, it seems that you and the compiler are in a bit of a row.

If you make separate functions for each case, it's easier to spot mistakes.

 switch (myState)
   {
    case STATE_armed:
    {
      armed();
      break;
     }
    case STATE_kaboom:
    {
     kaboom();
     break; //<<<<<<<<<< you forgot this break
    }
    case STATE_defused:
    {
     defused();
     break;
    }
  }

ive changed things it likes it then i get cases label not within a switch statemen

int statled = 13;
int clearled = 12;
int badwire1 = 2;
int badwire2 = 3;
int rightwire = 4;
int badwire3 = 5;
int rightwire2 = 6;

int ledState = LOW;

enum State { STATE_armed, STATE_kaboom, STATE_defused, } myState;



void setup() {

  Serial.println(9600);
  pinMode(statled, OUTPUT);
  pinMode(clearled, OUTPUT);
  pinMode(rightwire, INPUT_PULLUP);
  pinMode(rightwire2, INPUT_PULLUP);
  pinMode(badwire1, INPUT_PULLUP);
  pinMode(badwire2, INPUT_PULLUP);
  pinMode(badwire3, INPUT_PULLUP);


}

void loop() {
  switch (myState)
  {
      Serial.println("Armed");
      setStateTo(STATE_armed);
  }
    case STATE_armed:
      {
        // OUTPUTS:
        digitalWrite(statled, HIGH);
        delay(10);
        digitalWrite(statled, LOW);
        delay(100);

        digitalWrite(clearled, LOW);
        delay(0);
        digitalWrite(clearled, LOW);
        delay(0);
      }
      //INPUTS:
      {
        Serial.println("Armed");
        setStateTo(STATE_armed);
      }

      {
        if (digitalRead(badwire1) == HIGH || digitalRead(badwire2) == HIGH || digitalRead(badwire3) == HIGH)
        {
          Serial.println("Kaboom");
          setStateTo (STATE_kaboom);
        }
        else if (digitalRead(rightwire) == HIGH || digitalRead(rightwire2) == HIGH)
        {
          Serial.println("Defused");
          setStateTo (STATE_defused);
        }

        {
          Serial.println("Kaboom");
          setStateTo (STATE_kaboom);
        }

        {
          if (ledState == LOW)
          {
            ledState = HIGH;
          }

          {
            ledState = LOW;
          }
          statled = LOW;
        }
        digitalWrite(clearled, statled);
        break;
      }
    case STATE_kaboom:
      {
        // OUTPUTS:
        digitalWrite(statled, HIGH);
        delay(100);
        digitalWrite(statled, LOW);
        delay(10);
        digitalWrite(clearled, HIGH);
        delay(10);
        digitalWrite(clearled, LOW);
        delay(100);
      }
    case STATE_defused:
      {
        // OUTPUTS:
        digitalWrite(statled, HIGH);
        delay(0);
        digitalWrite(statled, LOW);
        delay(0);
        digitalWrite(clearled, HIGH);
        delay(1);
        digitalWrite(clearled, LOW);
        delay(1);
      }
  }
}