Pages: [1]   Go Down
Author Topic: help with spdt toggle switch  (Read 958 times)
0 Members and 1 Guest are viewing this topic.
Rome, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5666
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Please post your code...
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Rome, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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);
}
Logged

Valencia, Spain
Offline Offline
Faraday Member
**
Karma: 150
Posts: 5666
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

What does this do?

Code:
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);
}
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Rome, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

Rome, Italy
Offline Offline
Newbie
*
Karma: 0
Posts: 19
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!! smiley

Antonello
Logged

Pages: [1]   Go Up
Jump to: