How to use an ON-OFF-ON switch to switch on 2 different LEDs?
yes
the switch stays hard..sorry bad english
This is NOT a tutorial.
I don't understand what you mean. can you give examples. this first time I use Arduino for a school project. I am sincerely sorry for causing any problem.
:)...Thank you
Hi..do you think my coding is correct?
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin1 = 13; // the number of the LED pin
const int ledPin2 = 12;
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin1, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
pinMode(ledPin2, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
digitalWrite(ledPin1, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin1, LOW);
}
if (buttonState2 == HIGH) {
// turn LED on:
digitalWrite(ledPin2, HIGH);
} else {
// turn LED off:
digitalWrite(ledPin2, LOW);
}
}
These could all be byte, vs int, to take up less space in memory. Anything that will stay 0 to 255 can be an byte.
const int buttonPin1 = 2; // the number of the pushbutton pin
const int buttonPin2 = 3;
const int ledPin1 = 13; // the number of the LED pin
const int ledPin2 = 12;
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
You also need to read buttonPin2:
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
as you are acting on its state:
if (buttonState2 == HIGH) {
Hi..do you think my coding is correct?
No, it is way too long
const byte buttonPin1 = 2;
const byte buttonPin2 = 3;
const byte ledPin1 = 13;
const byte ledPin2 = 12;
void setup()
{
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
}
void loop() {
digitalWrite(ledPin1, digitalRead(buttonPin1));
digitalWrite(ledPin2, digitalRead (buttonPin2));
}
thank guys..I can`t say I fully understand but it ok...try my best