Hi!
I have a code i need help with.
The code i have now Lights up a LED when i push a button and turn it off when i push it again.
But i want 3 separate channels (3 buttons and 3 LEDs) I have been tried and tried without luck.
I will be wery happy if somebody can help me to edit the code.
The thing is that, I would have 3 channels that are completely independent of each other.
sorry my bad english
The code is in the pastebin link ![]()
The code is in the pastebin link
I will look at it when you post it here.
Thank you!
/* switch
*
* Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
* press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's
* a minimum delay between toggles to debounce the circuit (i.e. to ignore
* noise).
*
* David A. Mellis
* 21 November 2006
*/
int inPin = 2; // the number of the input pin
int outPin = 13; // the number of the output pin
int state = HIGH; // the current state of the output pin
int reading; // the current reading from the input pin
int previous = LOW; // the previous reading from the input pin
// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0; // the last time the output pin was toggled
long debounce = 200; // the debounce time, increase if the output flickers
void setup()
{
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
// if the input just went from LOW and HIGH and we've waited long enough
// to ignore any noise on the circuit, toggle the output pin and remember
// the time
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
time = millis();
}
digitalWrite(outPin, state);
previous = reading;
}
Thank you. Much easier to access.
The obvious way to do what you want is to repeat the code 3 times using different variables for each button/LED combination. Start by using the code that you have and renaming all the variables with a zero on the end, such as state0, inPin0 etc. Make sure that the code works. Now add a second set of variables with the same names but with a 1 on the end, such as state1, inPin1 etc and separate code to read and handle the input and timing. Make sure that the code works. Now add a third set of variables etc with a 2 at the end of the names and make sure that it works.
By now you will have lots of lines of code with similar variable names all doing the same thing. There must be a better way, and there is. Read up on arrays. Declare all your variables as arrays with 3 levels. This way you will have state[0], state[1] and state[2] etc. Now put one set of the code that reads inputs and deals with outputs into a for loop that runs from 0 to 2 and accesses the array variables using the for loop variable. Now you have a much smaller program and have learned about arrays and for loops.
This code adapt from @Robin2 "Demonstration code for several things at the same time"
http://forum.arduino.cc/index.php?topic=223286.0
// This code adapt from Robin2 "Demonstration code for several things at the same time"
// http://forum.arduino.cc/index.php?topic=223286.0
// this sketch does the following
// it turns the Led (buttonLed connected to pin 11,12,13) on or off whenever a button
// connected to pin 2,3,4 is pressed
// One leg of each LED should be connected to the relevant pin and the other leg should be connected to a
// resistor of 470 ohms or more and the other end of the resistor to the Arduino GND.
// If the LED doesn't light its probably connected the wrong way round.
// The use of millis() to manage the timing of activities
// The definition of all numbers used by the program at the top of the sketch where
// they can easily be found if they need to be changed
//========================================
// --------CONSTANTS (won't change)---------------
const int buttonLed1_Pin = 13; // the pin numbers for the LEDs
const int buttonLed2_Pin = 12;
const int buttonLed3_Pin = 11;
const int buttonPin1 = 2; // the pin number for the button
const int buttonPin2 = 3; // the pin number for the button
const int buttonPin3 = 4; // the pin number for the button
const int buttonInterval = 300; // number of millisecs between button readings
//------------ VARIABLES (will change)---------------------
byte buttonLed1_State = LOW; // used to record whether the LEDs are on or off
byte buttonLed2_State = LOW; // LOW = off
byte buttonLed3_State = LOW;
unsigned long currentMillis = 0; // stores the value of millis() in each iteration of loop()
unsigned long previousButtonMillis = 0; // time when button press last checked
//========================================
void setup() {
Serial.begin(9600);
Serial.println("Starting LED Button"); // so we know what sketch is running
// set the Led pins as output:
pinMode(buttonLed1_Pin, OUTPUT);
pinMode(buttonLed2_Pin, OUTPUT);
pinMode(buttonLed3_Pin, OUTPUT);
// set the button pin as input with a pullup resistor to ensure it defaults to HIGH
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
}
//========================================
void loop() {
// Notice that none of the action happens in loop() apart from reading millis()
// it just calls the functions that have the action code
currentMillis = millis(); // capture the latest value of millis()
// this is equivalent to noting the time from a clock
// use the same time for all LED flashes to keep them synchronized
readButton(); // call the functions that do the work
switchLeds();
}
//========================================
void switchLeds() {
// this is the code that actually switches the LEDs on and off
digitalWrite(buttonLed1_Pin, buttonLed1_State);
digitalWrite(buttonLed2_Pin, buttonLed2_State);
digitalWrite(buttonLed3_Pin, buttonLed3_State);
}
//========================================
void readButton1() {
// this only reads the button state after the button interval has elapsed
// this avoids multiple flashes if the button bounces
// every time the button is pressed it changes buttonLed_State causing the Led to go on or off
// Notice that there is no need to synchronize this use of millis() with the flashing Leds
if (millis() - previousButtonMillis >= buttonInterval) {
if (digitalRead(buttonPin1) == LOW) {
buttonLed1_State = ! buttonLed1_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
}
if (digitalRead(buttonPin2) == LOW) {
buttonLed2_State = ! buttonLed2_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
}
if (digitalRead(buttonPin3) == LOW) {
buttonLed3_State = ! buttonLed3_State; // this changes it to LOW if it was HIGH
// and to HIGH if it was LOW
}
previousButtonMillis = millis();
}
}
//========================================END
I cant make it work ![]()
A friend helped me and the code is compiling without problem but it doesent work.
The button make no differense when i press it and the led in pin 7 is contant on.
int inPin = 2;
int inPinTwo = 3;
int inPinThree = 4;
int outPin = 5;
int outPinTwo = 6;
int outPinThree = 7;
int state = HIGH;
int reading;
int previous = LOW;
int stateTwo = HIGH;
int readingTwo;
int previousTwo = LOW;
int stateThree = HIGH;
int readingThree;
int previousThree = LOW;
long time = 0;
long debounce = 200;
void setup()
{
pinMode(inPin, INPUT);
pinMode(inPinTwo, INPUT);
pinMode(inPinThree, INPUT);
pinMode(outPin, OUTPUT);
pinMode(outPinTwo, OUTPUT);
pinMode(outPinThree, OUTPUT);
}
void loop()
{
reading = digitalRead(inPin);
readingTwo = digitalRead(inPinTwo);
readingThree = digitalRead(inPinThree);
if (reading == HIGH && previous == LOW && millis() - time > debounce) {
if (state == HIGH)
state = LOW;
else
state = HIGH;
{
if (readingTwo == HIGH && previousTwo == LOW && millis() - time > debounce) {
if (stateTwo == HIGH)
stateTwo = LOW;
else
stateTwo = HIGH;
}
{
if (readingThree == HIGH && previousThree == LOW && millis() - time > debounce) {
if (stateThree == HIGH)
stateThree = LOW;
else
stateThree = HIGH;
time = millis();
}
digitalWrite(outPin, state);
digitalWrite(outPinTwo, stateTwo);
digitalWrite(outPinThree, stateThree);
previous = reading;
}
}
}
}
previous = reading;This is in the wrong place, and what about the previous readings of the other two inputs ?
I see that you did not follow my advice to name the variables with a 0, 1 or 2 at the end. You can make the program work by using two and three but using 0, 1 and 2 makes the change to using arrays to hold the variables much more obvious.