//well, this thing is triggering onece too many times, should do till hardID-1 ig;
//left off at hardID somewhere
#define button 4
#define red 12 //D6
#define green 14 //D7
#define blue 13 //D5
//#include <ESP8266WiFi.h>
//#include <espnow.h>
#include <EEPROM.h>
byte epromDATA[10];
//time
unsigned long tOld=0;
//ESP-NOW
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
typedef struct message {
byte mode;
byte brightnes;
} message;
message mainData;
//RGB capabilaties
byte current_mode=0;
byte bright=255;
//general
bool host=false;
byte hardID=0;
//blink hardID
unsigned long tBlink=0;
byte times_blinkled=0;
byte times_off=0;
bool led_on=false;
unsigned int led_on_time=50;
unsigned int led_off_time=200;
unsigned int break_time=700;
//blink blink_single_tripple
unsigned long tBlink=0;
byte times_blinkled=0;
byte times_off=0;
bool led_on=false;
unsigned int led_on_time=50;
unsigned int led_off_time=200;
unsigned int break_time=700;
//button_check
unsigned long tHold;
bool button_check_buton_ready=true;
bool buttonState=false;
bool button_check(){
buttonState = analogRead(button);
if(buttonState == LOW & button_check_buton_ready){
button_check_buton_ready=false;
tHold=millis();
return 1;
}
if(buttonState == HIGH){
button_check_buton_ready=true;
return 0;
}
return 0;
}
void blink_hardID(){
Serial.print("hardid ");
if(millis()-tBlink>=((hardID*led_on_time)+(hardID*led_off_time)+break_time)){ //reset timer if one full blink cicle has passed
tBlink=millis();//probably need to add some kind of action to switch to blink single/tripple
times_off=0;
times_blinkled=0;
led_on=false;
Serial.println("TIMER RESET");
}
//times_blinkled==0 ? times_blinkled_minusone=0 : times_blinkled_minusone=times_blinkled-1; // not sure if sintax is ok here
if(times_blinkled<=hardID){
if(led_on){
Serial.print(millis()-tBlink);
Serial.println(" on");
if(millis()-tBlink>=(times_blinkled*led_on_time)+(times_off*led_off_time)){
led_on=false;
digitalWrite(LED_BUILTIN,HIGH);
times_off++;
}
}
else{
Serial.print(millis()-tBlink);
Serial.println(" off");
if(millis()-tBlink>=(times_blinkled*led_on_time)+(times_off*led_off_time)){
times_blinkled++;
led_on=true;
digitalWrite(LED_BUILTIN,LOW);
}
}
}
else{
Serial.print(millis()-tBlink);
Serial.println(" BREAK");
}
}
void blink_single_tripple(){
Serial.print("hardid ");
if(millis()-tBlink>=((hardID*led_on_time)+(hardID*led_off_time)+break_time)){ //reset timer if one full blink cicle has passed
tBlink=millis();//probably need to add some kind of action to switch to blink single/tripple
times_off=0;
times_blinkled=0;
led_on=false;
Serial.println("TIMER RESET");
}
//times_blinkled==0 ? times_blinkled_minusone=0 : times_blinkled_minusone=times_blinkled-1; // not sure if sintax is ok here
if(times_blinkled<=hardID){
if(led_on){
Serial.print(millis()-tBlink);
Serial.println(" on");
if(millis()-tBlink>=(times_blinkled*led_on_time)+(times_off*led_off_time)){
led_on=false;
digitalWrite(LED_BUILTIN,HIGH);
times_off++;
}
}
else{
Serial.print(millis()-tBlink);
Serial.println(" off");
if(millis()-tBlink>=(times_blinkled*led_on_time)+(times_off*led_off_time)){
times_blinkled++;
led_on=true;
digitalWrite(LED_BUILTIN,LOW);
}
}
}
else{
Serial.print(millis()-tBlink);
Serial.println(" BREAK");
}
}
void settings_change() {
Serial.println("changing settings");
if(host){//If host change brightness
}
else{//If pleb change hardID and single/tripple
//blink hardID
while(true){
delay(1);
blink_hardID();
}
if(led_on){
}
else{
}
}
tHold=0;
return;
}
void setup() {
pinMode(button, INPUT_PULLUP);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
EEPROM.begin(10);
EEPROM.get(1, epromDATA);
if(!digitalRead(button)){
tOld=millis();
Serial.println("SWITCHING TO HOST ON BOOT");//Flip host if entered this mode
epromDATA[1]=5;
epromDATA[2]=0;
epromDATA[3]=169;
epromDATA[4]=!epromDATA[4];
Serial.println(" EEPROM host FLIPPED");
EEPROM.put(1, epromDATA);
EEPROM.commit();
while(true){
delay(1);
if(digitalRead(button)) break; //exit if button pressed
if(millis()-tOld>=10000){ //force clear eeprom to default, if 10s passed
epromDATA[1]=5;
epromDATA[2]=0;
epromDATA[3]=169;
epromDATA[4]=false;
Serial.println("FORCE EEPROM clear");
EEPROM.put(1, epromDATA);
EEPROM.commit();
break;
}
}
}
delay(1000);
Serial.println();
Serial.println();
EEPROM.get(1, epromDATA);
Serial.println(host);
if(epromDATA[1]!=5){
epromDATA[1]=5;
epromDATA[2]=0;
epromDATA[3]=169;
epromDATA[4]=false;
Serial.println("cleared");
EEPROM.put(1, epromDATA);
EEPROM.commit();
EEPROM.get(1, epromDATA);
}
hardID=epromDATA[2];
bright=epromDATA[3];
host=epromDATA[4];
/*
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
if(host){
//esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
//esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
else{
if(hardID==0)hardID=1;
}
*/
hardID=3;
Serial.print("hostStatus: ");
Serial.println(host);
Serial.println(hardID);
Serial.println(bright);
// host/not gotten from eeprom
}
void loop() {
button_check();
if(button_check_buton_ready==false & millis()-tHold>=1000) { //if button pressed for 3000ms, go into hard id selection;
settings_change();
}
if(host){
if (buttonState) {
mainData.mode=69;
mainData.brightnes=42;
//esp_now_send(broadcastAddress, (uint8_t *) &mainData, sizeof(mainData));
Serial.println("STATE 1");
}
else {
mainData.mode=42;
mainData.brightnes=69;
//esp_now_send(broadcastAddress, (uint8_t *) &mainData, sizeof(mainData));
Serial.println("STATE 2");
}
delay(2000);
}
else{
Serial.println("pleb");
delay(2000);
}
delay(1);
}