Go Down

Topic: help with spdt toggle switch (Read 995 times) previous topic - next topic

allo90

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

fungus

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

allo90

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

Code: [Select]
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);
}

fungus

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

What does this do?

Code: [Select]

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

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

allo90

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

allo90

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!!! :)

Antonello

Go Up
 

Quick Reply

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Note: this post will not display until it's been approved by a moderator.
Name:
Email:

shortcuts: alt+s submit/post or alt+p preview