help with spdt toggle switch

Hello everybody,
My name is Antonello, I'm from Italy and I've started messing around with Arduino a few months ago. The project I'm currently trying to realize is a sound/laser trigger for my camera. Since I had to start from the very "abc" in electronics, I encounter problems every now and then, which I managed to solve on my own till now. This time I need some help with a toggle switch: it is an spdt type switch, two positions, both ON. In my idea this toggle should make the arduino switch between two parts of the program, depending on its position. The central pin is connected to +5v, wherease every external pin is connected to one of Arduino's digital input pins and to GND through a 10k resistor. I wanted Arduino to check the status of the switch and do something like: "if pin x = HIGH, do this; if Pin y = HIGH, do that" and then report which of the two gave HIGH on serial monitor. Problem is that Arduino seems to read HIGH from both the pins, at the same time. What am I doing wrong? How can I fix it?
Thanks to everyone who wants to help me,

Antonello

Please post your code...

I think I have a new problem now: Serial monitor shows nothing at all...
Here's the code:

int toggle1 = 2;
int toggle2 = 3;


void setup() {
   Serial.begin(9600);
  pinMode(toggle1, INPUT);
  pinMode(toggle2, INPUT);
}

void loop() {
 digitalRead(toggle1);
 digitalRead(toggle2);
   if(toggle1 == HIGH && toggle2 == LOW){
     Serial.println("2"); 
   }
   else if (toggle1 == LOW && toggle2 == HIGH){
     Serial.println("3");
   }
  delay(1);
}

"digitalRead()" doesn't work like that, you have to use the result of the function.

What does this do?

int toggle1 = 2;
int toggle2 = 3;

void setup() {
  Serial.begin(9600);
  pinMode(toggle1, INPUT);
  pinMode(toggle2, INPUT);
}

void loop() {
 int t1 = digitalRead(toggle1);
 Serial.print("toggle1="); 
 Serial.print(t1); 

 int t2 = digitalRead(toggle2);
 Serial.print("  toggle2="); 
 Serial.print(t2); 

 Serial.println(); 
 delay(100);
}

Hello fungus, thanks for your answer. Someone from the italian section of the forum answered me as well, and I think you both have guessed what was the problem: I didn't declare the variable in which the result of digitalread function has to be stored. Now I can't try the code you've posted but as soon as I can I'll see what I get!!

Antonello

Ok, I've just finished testing the circuit with the fixed version of the code: it works perfectly, many thanks!!! now I can go on and complete the project!!! :slight_smile:

Antonello

hello sir/madam
i have a two programs, remote control and temperature control, and i want to control these programs with a spdt toggle switch in the arduino, this spdt is a two positions, both ON.. however, i dont know to how to start a code with my two programs with the use of a spdt toggle switch...
thank you!!

josephviloantomas:
hello sir/madam
i have a two programs, remote control and temperature control, and i want to control these programs with a spdt toggle switch in the arduino, this spdt is a two positions, both ON.. however, i dont know to how to start a code with my two programs with the use of a spdt toggle switch...
thank you!!

Please start a new thread asking your question.

.