|
/* This project implements a multi-color (LED) button with the following specification: |
|
* |
|
* - Button starts as off, representing everything is good |
|
* - A click changes the button to green and represents a request to start filling up a new order |
|
* - The warehouse control can send a "receive" event, which turns color to light blue |
|
* - Clicking button for more than 3sec resets button to off |
|
* - Double click sets the button to red |
|
* |
|
*/ |
|
|
|
// You must install the IOTile Bridge. |
|
// See https://github.com/iotile/poduino/blob/master/docs/installation/iotile-arduino-bridge.md |
|
// |
|
#include <IOTileBridgeMega.h> |
|
|
|
#define kAttentionPin 39 |
|
#define kEventReceivedPin 3 |
|
#define kIotileStreamId 10 |
|
|
|
#define kButtonPin 8 |
|
|
|
#define kRedPin 7 // Gren Cable |
|
#define kGreenPin 5 // Purple Cable |
|
#define kBluePin 6 // Yellow Cable |
|
|
|
#define kStateOff 0 |
|
#define kStateWhite 1 // Green for now |
|
#define kStateLBlue 2 // Light Blue |
|
#define kStateRed 3 |
|
|
|
#define kDebounceDelay 50 // the debounce time; increase if the output flickers |
|
|
|
unsigned int currentState = kStateOff; |
|
|
|
// onEvent Callback |
|
void onEventReceived(unsigned int event); |
|
// Bridge instantiation |
|
IOTileBridge bridge(kAttentionPin, onEventReceived); |
|
|
|
// Variables will change: |
|
int buttonState = HIGH; // the current reading from the input pin |
|
int lastButtonState = LOW; // the previous reading from the input pin |
|
|
|
// the following variables are unsigned longs because the time, measured in |
|
// milliseconds, will quickly become a bigger number than can be stored in an int. |
|
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled |
|
unsigned short fastClickCount = 0; |
|
|
|
unsigned long button_low_t0 = millis(); |
|
|
|
|
|
/* |
|
* Description: Set LEDs to produce given color. |
|
* Unfortunately, cannot really produce all colors with these LEDs |
|
*/ |
|
void rgb(unsigned int red, unsigned int green, unsigned int blue) { |
|
Serial.print("R="); |
|
Serial.print(red); |
|
Serial.print(" G="); |
|
Serial.print(green); |
|
Serial.print(" B="); |
|
Serial.println(blue); |
|
Serial.println("----------------"); |
|
|
|
analogWrite(kBluePin, blue); |
|
analogWrite(kGreenPin, green); |
|
analogWrite(kRedPin, red); |
|
} |
|
|
|
/* |
|
* Description: Given a state, set the right LEDs to achieve a given color. |
|
* Also broadcast the state via the IOTile controller |
|
*/ |
|
void handleButtonAction(unsigned int state) { |
|
|
|
if (state == kStateOff) { |
|
Serial.println("StateOff - Off"); |
|
rgb(255, 255, 255); |
|
} else if (state == kStateWhite) { |
|
Serial.println("StateWhite - Green"); |
|
rgb(255, 200, 250); |
|
} else if (state == kStateLBlue) { |
|
Serial.println("StateLBlue - Light Blue"); |
|
rgb(255, 244, 220); |
|
} else if (state == kStateRed) { |
|
Serial.println("StateRed - Red"); |
|
rgb(100, 255, 255); |
|
} |
|
bridge.sendEvent(kIotileStreamId, state); |
|
} |
|
|
|
/* |
|
* Description: Return true if the button has been pressed |
|
*/ |
|
boolean buttonPressed(){ |
|
|
|
boolean press = LOW; |
|
if (digitalRead(kButtonPin)==LOW) { |
|
press = HIGH; |
|
} |
|
|
|
return press; |
|
} |
|
|
|
/* |
|
* Description: This function will be called every time data is sent via the controller (BLE) |
|
* With a computer with IOTile coretools installed, do |
|
* |
|
* iotile hw --port=bled112 connect 0x1c8 get 11 send_event 1 |
|
* |
|
* This will set the state to kStateLBlue and turn on the LEDs to blue |
|
*/ |
|
void onEventReceived(unsigned int event) |
|
{ |
|
// Event number represents the desired state |
|
// Serial.println("==>"); |
|
// Serial.print("onEventReceived: "); |
|
// Serial.println(event); |
|
if (event) { |
|
currentState = kStateLBlue; |
|
} else { |
|
currentState = kStateOff; |
|
} |
|
handleButtonAction(currentState); |
|
} |
|
|
|
|
|
/* |
|
* Description: Arduino Setup function |
|
*/ |
|
void setup() { |
|
|
|
pinMode(kEventReceivedPin, OUTPUT); |
|
digitalWrite(kEventReceivedPin, LOW); |
|
|
|
bridge.begin(); |
|
|
|
// Arduino Debug Terminal |
|
Serial.begin(9600); |
|
|
|
pinMode(kButtonPin, INPUT); |
|
pinMode(kBluePin, OUTPUT); |
|
pinMode(kGreenPin, OUTPUT); |
|
pinMode(kRedPin, OUTPUT); |
|
|
|
rgb(255, 255, 255); |
|
|
|
currentState = kStateOff; |
|
handleButtonAction(currentState); |
|
|
|
} |
|
|
|
/* |
|
* Description: Arduino Loop function |
|
*/ |
|
void loop() { |
|
|
|
// read the state of the switch into a local variable: |
|
int reading = digitalRead(kButtonPin); |
|
|
|
// check to see if you just pressed the button |
|
// (i.e. the input went from LOW to HIGH), and you've waited long enough |
|
// since the last press to ignore any noise: |
|
|
|
// If the switch changed, due to noise or pressing: |
|
if (reading != lastButtonState) { |
|
// reset the debouncing timer |
|
lastDebounceTime = millis(); |
|
} |
|
|
|
if ((millis() - lastDebounceTime) > kDebounceDelay) { |
|
// whatever the reading is at, it's been there for longer than the debounce |
|
// delay, so take it as the actual current state: |
|
|
|
// if the button state has changed: |
|
if (reading != buttonState) { |
|
buttonState = reading; |
|
|
|
// process new state if the new button state is HIGH |
|
if (buttonState == HIGH) { |
|
|
|
if (millis() - button_low_t0 >= 1800) { |
|
// Pressing button more than 3sec -> Off |
|
currentState = kStateOff; |
|
} else { |
|
if (fastClickCount >= 2) { |
|
currentState = kStateRed; |
|
} else { |
|
currentState = kStateWhite; |
|
} |
|
} |
|
handleButtonAction(currentState); |
|
|
|
} else { |
|
if (millis() - button_low_t0 < 500) { |
|
fastClickCount += 1; |
|
} else { |
|
fastClickCount = 1; |
|
} |
|
button_low_t0 = millis(); |
|
} |
|
} |
|
} |
|
|
|
// save the reading. Next time through the loop, it'll be the lastButtonState: |
|
lastButtonState = reading; |
|
|
|
bridge.checkReceivedEvents(); |
|
} |