// --------------------------------
// Ready-to-go or Smart go-to-lunch
// --------------------------------
// Values for myStatus:
const int OFF = -1;
const int RED = 0; // or "rosso"
const int GRN = 1; // or "verde"
const int YLW = 2; // or "giallo"
// Led IDs
const int USR = 0;
const int GRP = 1;
// Button pins
int buttPins[] = { D2, D0, D1 }; // R, G, Y
// Led pins
int ledPins[] = { A0, A1, A2, A3, A4, D5 }; // R, G, (B), R, G, (B)
int blueLed = D7;
int buzzer = A5;
// Variables
int myStatus = OFF;
int grn_count = 0;
int ylw_count = 0;
String myID;
int buttonState[3]; // the current reading from the input pin
int debounceDelay = 50; // button must be pushed for at least xx ms
int currTime = 0; // current uptime value
int lastTime = 0; // last update time
int dogTime = 3600; // watchdog expiration time
void setup() {
// Set pin modes
for (int thisPin = 0; thisPin < 6; thisPin++) {
pinMode(ledPins[thisPin], OUTPUT);
}
for (int thisPin = 0; thisPin < 3; thisPin++) {
pinMode(buttPins[thisPin], INPUT_PULLUP);
buttonState[thisPin] = HIGH;
}
pinMode(blueLed, OUTPUT);
pinMode(buzzer, OUTPUT);
// Test tone
tone(buzzer, 1800, 150);
delay(200);
tone(buzzer, 2500, 150);
// Test LED colors
for (int thisPin = 0; thisPin < 6; thisPin++) {
digitalWrite(ledPins[thisPin], HIGH);
delay(300);
digitalWrite(ledPins[thisPin], LOW);
}
blinkBlue(2);
// Use local IP as ID
IPAddress localIP = WiFi.localIP();
myID = String(localIP[3]);
// Subscribe to events
Particle.subscribe("ioPranzoStatus", eventHandler);
// Declare cloud variables
Particle.variable("status", myStatus);
// Particle.variable("address", myID);
// Particle.variable("green", grn_count);
// Particle.variable("yellow", ylw_count);
}
void loop() {
// Any button pushed? Call buttPushed()
for (int i=0; i<3; i++) {
// read the state of the switch into a local variable:
int reading = digitalRead(buttPins[i]);
// check to see if you just pressed the button (i.e. the input went from HIGH to LOW),
// and you've waited long enough since the last press to ignore any noise:
if (reading == LOW) {
delay(debounceDelay);
reading = digitalRead(buttPins[i]);
}
else {
// Button is not pushed
buttonState[i] = HIGH;
}
if (reading == LOW && buttonState[i] == HIGH) {
buttPushed(i);
buttonState[i] = LOW;
}
}
// Timeout elapsed? Reset counters and myStatus, turn off all leds
currTime = Time.now();
if (currTime-lastTime > dogTime) {
setLed(USR, OFF);
setLed(GRP, OFF);
grn_count = ylw_count = 0;
myStatus = OFF;
blinkBlue(3);
// Update last operation time
lastTime = currTime;
}
}
void buttPushed(int button) {
switch (button) {
case RED:
if (myStatus != RED) {
setLed(USR, RED);
if (myStatus == GRN) {
Particle.publish("ioPranzoStatus", myID + ":" + "rossoVerde", 60, PUBLIC);
// ylw_count--;
// grn_count--;
}
else if (myStatus == YLW) {
Particle.publish("ioPranzoStatus", myID + ":" + "rossoGiallo", 60, PUBLIC);
// ylw_count--;
}
myStatus = RED;
}
break;
case GRN:
if (myStatus != GRN) {
setLed(USR, GRN);
// Publish state=GRN
if (myStatus != YLW) {
Particle.publish("ioPranzoStatus", myID + ":" + "giallo", 60, PUBLIC);
// ylw_count++;
delay(50);
}
myStatus = GRN;
Particle.publish("ioPranzoStatus", myID + ":" + "verde", 60, PUBLIC);
// grn_count++;
}
break;
case YLW:
if (myStatus != YLW) {
setLed(USR, YLW);
// Publish state=YLW
if (myStatus == GRN) {
// grn_count--;
Particle.publish("ioPranzoStatus", myID + ":" + "gialloVerde", 60, PUBLIC);
}
else {
// ylw_count++;
Particle.publish("ioPranzoStatus", myID + ":" + "giallo", 60, PUBLIC);
}
myStatus = YLW;
}
break;
}
}
void setLed(int led, int col) {
digitalWrite(ledPins[led*3], LOW);
digitalWrite(ledPins[led*3+1], LOW);
delay(150);
if (col < YLW) {
digitalWrite(ledPins[led*3+col], HIGH); // R or G
}
else {
digitalWrite(ledPins[led*3], HIGH); // R +
digitalWrite(ledPins[led*3+1], HIGH); // G = Y
}
}
void eventHandler(const char *event, const char *data) {
// Update last operation time
lastTime = Time.now();
if (strstr(data, myID) == 0) {
// Not my own event
blinkBlue(2);
}
else {
// My own event
blinkBlue(1);
}
if (strstr(data,"rossoVerde") != 0) {
// someone pushed red button while in green status
ylw_count--;
grn_count--;
}
else if (strstr(data,"rossoGiallo") != 0) {
// someone pushed red button while in yellow status
ylw_count--;
}
else if (strstr(data,"verde") != 0) {
// someone pushed green button
grn_count++;
}
else if (strstr(data,"gialloVerde") != 0) {
// someone pushed yellow button while in green status
grn_count--;
}
else if (strstr(data,"giallo") != 0) {
// someone pushed yellow button
ylw_count++;
}
else {
// if the data is something else, don't do anything.
blinkBlue(3);
}
// Calculate led 1 (group) color
if ((grn_count + ylw_count) == 0) {
// Off
setLed(GRP, OFF);
}
else if (grn_count == 1 && ylw_count == 1) {
// Green
setLed(GRP, GRN);
}
else if (grn_count == ylw_count && myStatus != RED) {
for (int i=0; i<grn_count; i++) {
// Green blink
setLed(GRP, GRN);
tone(buzzer,2100,200);
delay(200);
}
}
else if (grn_count == ylw_count) {
// Green
setLed(GRP, GRN);
}
else if (grn_count < ylw_count) {
// Yellow
setLed(GRP, YLW);
}
}
void blinkBlue(int times) {
for (int i=0; i<times; i++) {
digitalWrite(blueLed, HIGH);
delay(150);
digitalWrite(blueLed, LOW);
delay(50);
}
}
Comments
Please log in or sign up to comment.