quick question

hi,
im trying to learn logic statments, how do i assign the int val's to the three switch pins for my logic statments??

9a6PR2hU

int ledPin = 12; // LED to pin 12
int ledpin = 11; // led to pin 13
int switchPin = 4; // switch is connected to pin 4
int switchPin = 3; // switch is connected to pin 3
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status
int val; // how do i assign these two pin 3???
int val; // how do i assign this to pin 2???

thank you

You can not have multiple variables of the same name. You want to look at the digitalRead() function.

hi paul thanks for the help
cant i call them...

int val1, and assign it to int switchPin = 2
int val2, int switchPin = 3
int val3, int switchPin = 4

then use the val1,2,3 in logic statement later ?

thank you

Perhaps this will help:
http://arduino.cc/en/Reference/DigitalRead

  • Scotty

cant i call them...

Yes, you can use suffixes to make the names unique.

I don't recommend that, though. There is some reason that you are attaching three switches. Name the pin variables and the value variables to reflect the purpose of the switch, not the number of the pin it is attached to.

If the switches are to increment, decrement, and set a value, name the pin variables incPin, decPin, and setPin, for instance.

I'm not sure what you mean by "and assign it to". You want to have something like this:

int incPin = 2;
int decPin = 3;
int setPin = 4;

void loop()
{
   int incVal = digitalRead(incPin);
   int decVal = digitalRead(decPin);
   int setVal = digitalRead(setPin);

ok will give my head a good scratch and think about it
thank you

hi paul
not sure that would work with what im trying to do, my 1st project was to monitor 3 inputs if all the same state then i get one output to drive a relay really confused because in all the examples they seem to contain just one led and input would like to monitor multi inputs, and use logic to drive relays

jonisonvespa:
hi paul
not sure that would work with what im trying to do, my 1st project was to monitor 3 inputs if all the same state then i get one output to drive a relay really confused because in all the examples they seem to contain just one led and input would like to monitor multi inputs, and use logic to drive relays

/*

  • Switch and LED test program
    */

int ledPin = 12; // LED is connected to pin 12
int switchPin = 2; // switch is connected to pin 2
int val; // variable for reading the pin status

void setup() {
pinMode(ledPin, OUTPUT); // Set the LED pin as output
pinMode(switchPin, INPUT); // Set the switch pin as input
}

void loop(){
val = digitalRead(switchPin); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(ledPin, HIGH); // turn LED on
}
if (val == HIGH) { // check if the button is not pressed
digitalWrite(ledPin, LOW); // turn LED off
}
}

its so frustrating, i under stand all of this program, but i really want to monitor 3 + inputs on multi switchpins. and have different outputs using logic statments

i really want to monitor 3 + inputs on multi switchpins.

If you can explain what that means, maybe we can help.

ok here goes, i would like to monitor 3 or more inputs using switchpins, then use the data to make outputs using the digitalwrite function,

thank you

What is a "switchpin"?
I've never heard the term before.

i got it from this tutorial here Arduino Tutorial - Lesson 5

That's a variable (it would be better as a constant) name.
What is it you're having problems with?

what my problem is an absolute mental block of getting more than 1 input/output in a program a, this is my first project i havnt worked out the logic in the loop yet.

/*

  • 2 switches monitoring two fans, healthly if both switches are low output switches a relay, if any input high an alarm sounds no output to relay.
    */

int greenled = 12; // green led lights if fan1 and fan2 are low connected to pin 12. (operates a relay)
int redled = 11; // red led lights if fan 1 or fan2 are high connected to pin 11. (sounds an alarm)
int fan1 = 3; // fan switch is connected to pin 3
int fan2 = 2; // fan switch is connected to pin 2
int val; // variable for reading the pin status

void setup() {
pinMode(greenled, OUTPUT); // Set the Greenlled pin as output-- this will light up if i get a low at fan1+fan2
pinMode(redled, OUTPUT); // Set the LED pin as output-- this will light up if i get a low at fan1+fan2
pinMode(fan1, INPUT); // Set the switch pin as input
pinMode(fan2, INPUT); // Set the switch pin as input
}

void loop(){
val = digitalRead(fan1); // read input value and store it in val
val = digitalRead(fan2); // read input value and store it in val
if (val == LOW) { // check if the button is pressed
digitalWrite(greenled, LOW); // turn LED on
}
if (val == HIGH) { // check if the button is not pressed
digitalWrite(greenled, LOW); // turn LED off
}
}

You're on the right track, but the second digitalRead overwrites the first.
Try a different variable.

Everything up to the loop() looks good. (You need to change the "val" declaration to two declarations to match this code)

void loop(){
  fan1val = digitalRead(fan1);   // read input value and store it in val
  fan2val = digitalRead(fan2);   // read input value and store it in val
  if (fan1val == LOW && fan2val == LOW) {               // check if both inputs show fan OK (Switch is pressed) 
    digitalWrite(greenled, HIGH);   // turn green on
    digitalWrite(redled, LOW);   //  .. and red off
  } else {   // we do not need to test the opposite -  we do not light green, we light red
    digitalWrite(greenled, LOW);    // turn green
    digitalWrite(redled, HIGH  // .. and red on
  }
}

Normally I do not like writing the code - I like giving a hint, but you wrote "having a mental block" and I can sympasise with that. The leap from "one" to "two" is sometimes hard.

Now if your switch/fan monitoring has a 3rd state, then you need to do "if then else if else" construction - or three seperate if statements. There are pro&con to

thanks very much Msquare, i will finish this tomorrow really appreciate everybody help so kind.

/*
 *  2 switches monitoring two fans, healthly if both switches are low output switches a relay, if any input high an alarm sounds no output to relay.
 */
 
int greenled = 12;            // green led lights if fan1 and fan2 are low connected to pin 12. (operates a relay) 
int redled = 11;              // red led lights if fan 1 or fan2 are high connected to pin 11.  (sounds an alarm)
int fan1 = 3;                 // fan switch is connected to pin 3
int fan2 = 2;                 // fan switch is connected to pin 2
int fan1val;                      // variable for reading the pin status
int fan2val;                      // variable for reading the pin status


void setup() {
  pinMode(greenled, OUTPUT);      // Set the Greenlled pin as output-- this will light up if i get a low at fan1+fan2
  pinMode(redled, OUTPUT);        // Set the LED pin as output-- this will light up if i get a low at fan1+fan2
  pinMode(fan1, INPUT);           // Set the switch pin as input
  pinMode(fan2, INPUT);           // Set the switch pin as input
}


 void loop(){
  fan1val = digitalRead(fan1);                           // read input value and store it in val
  fan2val = digitalRead(fan2);                           // read input value and store it in val
  if (fan1val == LOW && fan2val == LOW) {                 // check if both inputs show fan OK (Switch is pressed) 
    digitalWrite(greenled, HIGH);                         // turn green on
    digitalWrite(redled, LOW);                             //  .. and red off
  } else )                                                 // we do not need to test the opposite -  we do not light green, we light red
    digitalWrite(greenled, LOW);                            // turn green
    digitalWrite(redled, HIGH                                // .. and red on
  }
}

thanks for the help but when i load the program i get an error "primary expression before token, what does this mean??
thank you

Moderator edit: Please use tags when posting code.

} else )

BZZZZT

but when i load the program i get an error

No, you didn't get as far as loading the program - that's a compiler error, not a bootloader error.

ok will do as requested,

bzzzt does that mean wrong?