Creating an ON/OFF Switch

Hey all, i’m trying to create an ON/Off switch for a program I am running.

Simply put, if the switch is off it says “Turn Freddy On for a good time”

If the switch is on it runs the addition program. I’ve put my loop below. maybe someone can point out my error.

void loop() // run over and over again
{

digitalRead(switchPin);
delay (10000);

if (switchPin==LOW)
{Serial.println (“Turn Freddy on for some fun”);}

if (switchPin==HIGH)

{
{Serial.print("Read switch input 1: ");
Serial.println(digitalRead(switchPin));

int val= digitalRead(switchPin);

delay(1000);}

{Serial.print("Read switch input 2: ");
Serial.println(digitalRead(switch2Pin));

int val2= digitalRead(switch2Pin);

delay(1000);}

{Serial.print("Read switch input 3: ");
Serial.println(digitalRead(switch3Pin));
int val3= digitalRead(switch3Pin);

delay(1000);}

{Serial.print("Read switch input 4: ");
Serial.println(digitalRead(switch4Pin));
int val4= digitalRead(switch4Pin);
delay(1000);}

{Serial.print("Read switch input 5: ");
Serial.println(digitalRead(switch5Pin)); // Read the pin and display the value

int val5= digitalRead(switch5Pin);
delay(1000);}

int val2= digitalRead(switch2Pin);
int val3= digitalRead(switch3Pin);
int val4= digitalRead(switch4Pin);
int val5= digitalRead(switch5Pin);
int total= ((val2 + val3 + val4 + val5) * 10 );

{Serial.println(total);}

}

}

I think a better approach would be to use interrupts

http://www.arduino.cc/en/Reference/AttachInterrupt

Depends on how many buttons you would need to monitor though - the AVrs have limited hardware support

The below assumes the pins are in sequence. Delays will stall the program, so you might be better off using a counter, and when the counter gets to x, then check the pins.

int the_total = 0;
int switchPin = 0;

void setup()
{

}

void loop()                     // run over and over again
{
  digitalRead(switchPin);

  if (switchPin==LOW) {
      Serial.println ("Turn Freddy on for some fun");
       }



  if (switchPin==HIGH) {
      for (int the_pin=1; the_pin<= 5; the_pin++) {
            Serial.print("Read switch input ");
            Serial.print(the_pin);
            Serial.print(": ");
            int the_pin_val = digitalRead(the_pin);
            Serial.println(the_pin_val);
            the_total = (the_total + the_pin_val);
            delay(1000);
            }
      the_total=(the_total * 10);
      Serial.println(the_total);
      the_total=0;
}
}