//Test to control the leds on the livingwise 7button wall switch. // Initial work to replace the controller with an esp8266 (from unknown zigbee based module) // Copyright 2019 blog.nicolas.cx // Not for commercial use. No warranty expressed or implied. Use at your own risk. // // SH_CP; 11 const int clockPin = 12; //D6; //GPIO12 // DS; 15 const int dataPin = 13; //D7; //GPIO13 // ST_CP; 12 const int latchPin = 15; //D8; //GPIO15 const int button0 = 12; //D5; //GPIO14 const int button1 = 2; //D4; //GPIO2 const int button2 = 4; //D2; //GPIO4 const int button3 = 0; //D3; //GPIO0, pcb pulls up so no boot problems (handy access to get esp into program mode too) const int button4 = 5; //D1; //GPIO5 const int button5 = 16; //D0; //GPIO16 const int button6 = A0; //ADC0 unsigned long previousMillis = 0; const long interval = 1000; const int a0PressedThreshold = 500; //above this value = not pressed, lower = pressed. using a0/adc for 7th input. byte byteToSet = 0b11111111; byte currentNumber = 0; byte oldButtonValue = 0; #define PRINTBIN(Num) for (uint32_t t = (1UL<< (sizeof(Num)*8)-1); t; t >>= 1) Serial.write(Num & t ? '1' : '0'); void setup() { pinMode(clockPin, OUTPUT); pinMode(dataPin, OUTPUT); pinMode(latchPin, OUTPUT); pinMode(button0, INPUT); pinMode(button1, INPUT); pinMode(button2, INPUT); pinMode(button3, INPUT); pinMode(button4, INPUT); pinMode(button5, INPUT); pinMode(button6, INPUT); setHC595Byte(byteToSet); //turn off the leds. Serial.begin(115200); Serial.println("reset"); oldButtonValue = getButtons(); } void loop() { unsigned long currentMillis = millis(); if (currentMillis - previousMillis >= interval) { previousMillis = currentMillis; if(currentNumber > 6) currentNumber = 0; setLedToggle(currentNumber); Serial.print(currentNumber); Serial.print(" " ); PRINTBIN(byteToSet); Serial.println(""); currentNumber++; } byte buttonValue = getButtons(); if(buttonValue != oldButtonValue){ Serial.print("Button changed: "); PRINTBIN(buttonValue); Serial.println(""); for(byte i =0; i<=7; i++){ if(bitRead(buttonValue,i) == 1) setLedToggle(i); //do we want to check for 0>1 or 1>0 ? maybe later } oldButtonValue = buttonValue; } } byte getButtons(){ byte tmp = 0; if(digitalRead(button0)){ bitClear(tmp, 0); }else{ bitSet(tmp, 0); } if(digitalRead(button1)){ bitClear(tmp, 1); }else{ bitSet(tmp, 1); } if(digitalRead(button2)){ bitClear(tmp, 2); }else{ bitSet(tmp, 2); } if(digitalRead(button3)){ bitClear(tmp, 3); }else{ bitSet(tmp, 3); } if(digitalRead(button4)){ bitClear(tmp, 4); }else{ bitSet(tmp, 4); } if(digitalRead(button5)){ bitClear(tmp, 5); }else{ bitSet(tmp, 5); } if(analogRead(button6) > a0PressedThreshold){ bitClear(tmp, 6); }else{ bitSet(tmp, 6); } return tmp; } void setLedOn(byte number){ number++; //led1 is at bit position 1. d0 of hc595 is not used. bitClear(byteToSet, number); setHC595Byte(byteToSet); } void setLedOff(byte number){ number++; //led1 is at bit position 1. d0 of hc595 is not used. bitSet(byteToSet, number); setHC595Byte(byteToSet); } void setLedToggle(byte number){ if(bitRead(byteToSet, number+1)){ //remmeber 1 = off, due to the hc595 setup. setLedOn(number); }else{ setLedOff(number); } } void setHC595Byte(byte b){ digitalWrite(latchPin, LOW); shiftOut(dataPin, clockPin, MSBFIRST, b); digitalWrite(latchPin, HIGH); delay(10); }