Skip to main content
added 123 characters in body
Source Link
Ikbel
  • 620
  • 4
  • 13

Edit: Use the code below as it is, it's working for me:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int inPin = 22; 
int inPin2 = 26;
int outPin1 = 13;    
int outPin2 = 6;

int state1 = HIGH;
int state2 = HIGH;
int reading;     
int reading2;
int previous = HIGH; 
int previous2 = HIGH;

int debounce = 150;   // the debounce time, increase if the output flickers

//===============================================================================================
void doIt(int outPin, int state)
{
  Serial.print("Button Switch State Change:");
  if (state == HIGH) state = LOW;
  else state = HIGH;
  Serial.println(state);
  lcd.clear();
  
  if (outPin == outPin1) 
    lcd.print("Zone1|State: ");
  else if (outPin == outPin2)
    lcd.print("Zone2|State: ");
    
  lcd.print(state ? F("On") : F("Off"));
  Serial.print(state ? F("On") : F("Off"));
  digitalWrite(outPin, state);
}
//===============================================================================================
void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("System Activated");
  //
  lcd.setCursor(0, 1);
  lcd.print("Ready...");

  pinMode(inPin, INPUT);
  pinMode(outPin1, OUTPUT);
  pinMode(inPin2, INPUT);
  pinMode(outPin2, OUTPUT);

  Serial.begin(9600);
  Serial.println("System Activated");
  Serial.println("Made by Mateo Holguin");
  Serial.println("0 = Light is Off | 1 = Light is On");
  Serial.println("=======================================");
}

//===============================================================================================

void loop()
{
  reading = digitalRead(inPin);
  reading2 = digitalRead(inPin2);
  delay(debounce);

  if (reading == HIGH)
  {
    state1 = !state1;
    doIt(outPin1, state1);  
  }
  else if (reading2 == HIGH){
    state2 = !state2;
    doIt(outPin2, state2);
  }
}

Edit: Use the code below as it is, it's working for me:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int inPin = 22; 
int inPin2 = 26;
int outPin1 = 13;    
int outPin2 = 6;

int state1 = HIGH;
int state2 = HIGH;
int reading;     
int reading2;
int previous = HIGH; 
int previous2 = HIGH;

int debounce = 150;   // the debounce time, increase if the output flickers

//===============================================================================================
void doIt(int outPin, int state)
{
  Serial.print("Button Switch State Change:");
  if (state == HIGH) state = LOW;
  else state = HIGH;
  Serial.println(state);
  lcd.clear();
  lcd.print("Zone1|State: ");
  lcd.print(state ? F("On") : F("Off"));
  Serial.print(state ? F("On") : F("Off"));
  digitalWrite(outPin, state);
}
//===============================================================================================
void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("System Activated");
  //
  lcd.setCursor(0, 1);
  lcd.print("Ready...");

  pinMode(inPin, INPUT);
  pinMode(outPin1, OUTPUT);
  pinMode(inPin2, INPUT);
  pinMode(outPin2, OUTPUT);

  Serial.begin(9600);
  Serial.println("System Activated");
  Serial.println("Made by Mateo Holguin");
  Serial.println("0 = Light is Off | 1 = Light is On");
  Serial.println("=======================================");
}

//===============================================================================================

void loop()
{
  reading = digitalRead(inPin);
  reading2 = digitalRead(inPin2);
  delay(debounce);

  if (reading == HIGH)
  {
    state1 = !state1;
    doIt(outPin1, state1);  
  }
  else if (reading2 == HIGH){
    state2 = !state2;
    doIt(outPin2, state2);
  }
}

Edit: Use the code below as it is, it's working for me:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int inPin = 22; 
int inPin2 = 26;
int outPin1 = 13;    
int outPin2 = 6;

int state1 = HIGH;
int state2 = HIGH;
int reading;     
int reading2;
int previous = HIGH; 
int previous2 = HIGH;

int debounce = 150;   // the debounce time, increase if the output flickers

//===============================================================================================
void doIt(int outPin, int state)
{
  Serial.print("Button Switch State Change:");
  if (state == HIGH) state = LOW;
  else state = HIGH;
  Serial.println(state);
  lcd.clear();
  
  if (outPin == outPin1) 
    lcd.print("Zone1|State: ");
  else if (outPin == outPin2)
    lcd.print("Zone2|State: ");
    
  lcd.print(state ? F("On") : F("Off"));
  Serial.print(state ? F("On") : F("Off"));
  digitalWrite(outPin, state);
}
//===============================================================================================
void setup()
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("System Activated");
  //
  lcd.setCursor(0, 1);
  lcd.print("Ready...");

  pinMode(inPin, INPUT);
  pinMode(outPin1, OUTPUT);
  pinMode(inPin2, INPUT);
  pinMode(outPin2, OUTPUT);

  Serial.begin(9600);
  Serial.println("System Activated");
  Serial.println("Made by Mateo Holguin");
  Serial.println("0 = Light is Off | 1 = Light is On");
  Serial.println("=======================================");
}

//===============================================================================================

void loop()
{
  reading = digitalRead(inPin);
  reading2 = digitalRead(inPin2);
  delay(debounce);

  if (reading == HIGH)
  {
    state1 = !state1;
    doIt(outPin1, state1);  
  }
  else if (reading2 == HIGH){
    state2 = !state2;
    doIt(outPin2, state2);
  }
}
added 700 characters in body
Source Link
Ikbel
  • 620
  • 4
  • 13

This might be ugly, butEdit: Use the code below as it should do what you wantis, it's working for me:

int inPin1;#include //<LiquidCrystal.h>
LiquidCrystal Assignlcd(12, it11, to5, the4, First3, Button2);

int inPin2; // Assign it toinPin the= Second22; Button

int time1,inPin2 time2;
const= 26;
int debounceoutPin1 = 20; //13; Set it to the
int valueoutPin2 you= want6;

int state;
state1 = HIGH;
int outPin1;state2 //= AssignHIGH;
int itreading; to the First LED 
int outPin2;reading2;
int //previous Assign= itHIGH; to
int theprevious2 Second= LEDHIGH;

int reading1,debounce reading2;
int= previous1,150; previous2;

  /*/ Thethe restdebounce oftime, yourincrease declarationsif */the output flickers

//===============================================================================================
void doIt(int outPin, int state)
{
    Serial.print("Button Switch State Change:");
    if (state == HIGH) state = LOW;
    else state = HIGH;      
    Serial.println(state);
    lcd.clear();
    lcd.print("Zone1|State: ");
    lcd.print(state ? F("On") : F("Off"));
    Serial.print(state ? F("On") : F("Off"));
    digitalWrite(outPin, state);  
}
//===============================================================================================
void setup() 
{
  pinMode(outPin1,// OUTPUT);set up the LCD's number of columns and rows:
  pinModelcd.begin(outPin216, OUTPUT2);
  
 // Print 
}
a message to the LCD.
void loop lcd.print("System Activated")
{;
  //
  reading1 = digitalReadlcd.setCursor(inPin10, 1);
  reading2lcd.print("Ready...");

 = digitalReadpinMode(inPin2inPin, INPUT);
  if pinMode(reading1outPin1, ==OUTPUT);
 HIGH &&pinMode(inPin2, previous1INPUT);
 == LOWpinMode(outPin2, &&OUTPUT);

 millis Serial.begin(9600);
 - time1 >Serial.println("System debounceActivated"){;
  Serial.println("Made by doIt(outPin1Mateo Holguin");
  }
Serial.println("0 = elseLight ifis (reading2Off ==| HIGH1 &&= previous2Light ==is LOWOn");
 && millisSerial.println("=======================================");
}

//===============================================================================================

void loop()
{
 - time2reading >= debouncedigitalRead(inPin){;
  reading2 = doItdigitalRead(outPin2inPin2);
  }delay(debounce);

  if (reading == HIGH)
  {
  previous1  state1 = reading1;!state1;
    previous2doIt(outPin1, =state1); reading2; 
  }
  time1else =if millis(reading2 == HIGH);{
    time2state2 = millis!state2;
    doIt(outPin2, state2);
  }
}

This might be ugly, but it should do what you want:

int inPin1; // Assign it to the First Button
int inPin2; // Assign it to the Second Button

int time1, time2;
const int debounce = 20; // Set it to the value you want
int state;

int outPin1; // Assign it to the First LED
int outPin2; // Assign it to the Second LED

int reading1, reading2;
int previous1, previous2;

/* The rest of your declarations */

void doIt(int outPin)
{
    Serial.print("Button Switch State Change:");
    if (state == HIGH) state = LOW;
    else state = HIGH;      
    Serial.println(state);
    lcd.clear();
    lcd.print("Zone1|State: ");
    lcd.print(state ? F("On") : F("Off"));
    Serial.print(state ? F("On") : F("Off"));
    digitalWrite(outPin, state);  
}

void setup(){
  pinMode(outPin1, OUTPUT);
  pinMode(outPin2, OUTPUT);
  
   
}

void loop()
{
  
  reading1 = digitalRead(inPin1);
  reading2 = digitalRead(inPin2);
  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce){
    doIt(outPin1);
  }
  else if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce){
    doIt(outPin2);
  }    
    previous1 = reading1;
    previous2 = reading2;
    time1 = millis();
    time2 = millis();
  
}

Edit: Use the code below as it is, it's working for me:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

int inPin = 22; 
int inPin2 = 26;
int outPin1 = 13;    
int outPin2 = 6;

int state1 = HIGH;
int state2 = HIGH;
int reading;     
int reading2;
int previous = HIGH; 
int previous2 = HIGH;

int debounce = 150;   // the debounce time, increase if the output flickers

//===============================================================================================
void doIt(int outPin, int state)
{
  Serial.print("Button Switch State Change:");
  if (state == HIGH) state = LOW;
  else state = HIGH;
  Serial.println(state);
  lcd.clear();
  lcd.print("Zone1|State: ");
  lcd.print(state ? F("On") : F("Off"));
  Serial.print(state ? F("On") : F("Off"));
  digitalWrite(outPin, state);
}
//===============================================================================================
void setup() 
{
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("System Activated");
  //
  lcd.setCursor(0, 1);
  lcd.print("Ready...");

  pinMode(inPin, INPUT);
  pinMode(outPin1, OUTPUT);
  pinMode(inPin2, INPUT);
  pinMode(outPin2, OUTPUT);

  Serial.begin(9600);
  Serial.println("System Activated");
  Serial.println("Made by Mateo Holguin");
  Serial.println("0 = Light is Off | 1 = Light is On");
  Serial.println("=======================================");
}

//===============================================================================================

void loop()
{
  reading = digitalRead(inPin);
  reading2 = digitalRead(inPin2);
  delay(debounce);

  if (reading == HIGH)
  {
    state1 = !state1;
    doIt(outPin1, state1);  
  }
  else if (reading2 == HIGH){
    state2 = !state2;
    doIt(outPin2, state2);
  }
}
Source Link
Ikbel
  • 620
  • 4
  • 13

This might be ugly, but it should do what you want:

int inPin1; // Assign it to the First Button
int inPin2; // Assign it to the Second Button

int time1, time2;
const int debounce = 20; // Set it to the value you want
int state;

int outPin1; // Assign it to the First LED
int outPin2; // Assign it to the Second LED

int reading1, reading2;
int previous1, previous2;

/* The rest of your declarations */

void doIt(int outPin)
{
    Serial.print("Button Switch State Change:");
    if (state == HIGH) state = LOW;
    else state = HIGH;      
    Serial.println(state);
    lcd.clear();
    lcd.print("Zone1|State: ");
    lcd.print(state ? F("On") : F("Off"));
    Serial.print(state ? F("On") : F("Off"));
    digitalWrite(outPin, state);  
}

void setup(){
  pinMode(outPin1, OUTPUT);
  pinMode(outPin2, OUTPUT);
  
  
}

void loop()
{
  
  reading1 = digitalRead(inPin1);
  reading2 = digitalRead(inPin2);
  if (reading1 == HIGH && previous1 == LOW && millis() - time1 > debounce){
    doIt(outPin1);
  }
  else if (reading2 == HIGH && previous2 == LOW && millis() - time2 > debounce){
    doIt(outPin2);
  }    
    previous1 = reading1;
    previous2 = reading2;
    time1 = millis();
    time2 = millis();
  
}