arduino, x10, buttons, and lcd

this code is one night old. more to come from it but it’s not much now:

#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);
/*
brb 20091121
control 2 x10 devices (4 &2) via momentary switches on pins 10
and 12. x10 interface on pins 8 and 9 and a 16×2 lcd on 2, 3,
4, 5, 6, & 7. messages will be printed to the serial console and
to the lcd screen

hold down both buttons to shut all units off

*/
#include <x10.h>
#include <x10constants.h>

#define zcPin 8
#define dataPin 9

const int buttonPin2 = 10;
const int buttonPin = 12;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin
int buttonState = 0;
int buttonState2 = 0;         // variable for reading the pushbutton status
int buttoncount = 0;
int buttoncount2 = 0;
int resetcounterflag = 0;

// set up a new x10 instance:
x10 myHouse =  x10(zcPin, dataPin);

void setup() {
Serial.begin(9600);
// set up the LCD’s number of rows and columns:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print(“poweredByCoffee”);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
pinMode(buttonPin2, INPUT);
}

void loop() {
buttonState = digitalRead(buttonPin);
buttonState2 = digitalRead(buttonPin2);
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState == HIGH) {
delay(100);
if (buttonState2 == HIGH){
allUnitsOff();
}
else if (buttoncount == 0) {
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 0);
// print the light status:
lcd.print(“Desk OFF  “);
Serial.println(“Desk Light off:”);
// send a “lights off” command to workbench light 1 time:
myHouse.write(A, UNIT_4, 1);
myHouse.write(A, OFF, 1);
lcd.setCursor(12, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
buttoncount++;
delay(200);
if (resetcounterflag == 1){
resetcounterflag = 0;
buttoncount=0;
buttoncount2=0;
}
}
if(buttoncount == 2){
digitalWrite(ledPin, HIGH);
buttoncount=0;
lcd.setCursor(0, 0);
// print the light status:
lcd.print(“Desk ON   “);
Serial.println(“Desk Light on:”);
// send a “lights on” command to workbench light 1 time:
myHouse.write(A, UNIT_4, 1);
myHouse.write(A, ON, 1);
lcd.setCursor(12, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
delay(200);
}
if (buttonState2 == HIGH) {
delay(100);
if (buttonState == HIGH){
allUnitsOff();
}
else if (buttoncount2 == 0) {
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 0);
// print the light status:
lcd.print(“LR OFF     “);
Serial.println(“Living Room off:”);
// send a “lights off” command to workbench light 1 time:
myHouse.write(A, UNIT_2, 1);
myHouse.write(A, OFF, 1);
lcd.setCursor(12, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
buttoncount2++;
delay(200);
if (resetcounterflag == 1){
resetcounterflag = 0;
buttoncount=0;
buttoncount2=0;
}
}
if(buttoncount2 == 2){
digitalWrite(ledPin, HIGH);
buttoncount2=0;
lcd.setCursor(0, 0);
// print the light status:
lcd.print(“LR ON      “);
Serial.println(“Living Room on:”);
// send a “lights on” command to workbench light 1 time:
myHouse.write(A, UNIT_2, 1);
myHouse.write(A, ON, 1);
lcd.setCursor(12, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
delay(200);
}
//do nothing
lcd.setCursor(0, 1);
lcd.print(”  benduino x10″);
}

void allUnitsOff() {
digitalWrite(ledPin, LOW);
lcd.setCursor(0, 0);
// print the light status:
lcd.print(“ALL UNITS OFF”);
Serial.println(“All Units off:”);
// send a “lights off” command to workbench light 1 time:
myHouse.write(A, ALL_UNITS_OFF, 1);
lcd.setCursor(12, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
resetcounterflag=1;
delay(100);
return;
}

Leave a Reply