Added TableSoccer_LedStrip: Able to display Flags.

This commit is contained in:
Nicolas Bachschwell 2016-07-10 12:01:46 +02:00
parent 9b44c84696
commit 8555b29bf4

View File

@ -0,0 +1,390 @@
#include <Arduino.h>
#include <Homie.h>
#include <Adafruit_NeoPixel.h>
#include <sstream>
#ifdef __AVR__
#include <avr/power.h>
#endif
// Which pin on the NodeMCU is connected to the NeoPixels?
// On a Trinket or Gemma we suggest changing this to 1
#define PIN 14
// How many NeoPixels are attached to the Arduino?
#define NUMPIXELS 31
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
int blueToSave = 0;
int redToSave = 0;
bool flagIncoming = false;
bool shootout = false;
int color = 0;
int flag[3][3];
int stillToSleep;
HomieNode nodeGoal("goals", "goals");
HomieNode nodeFlag("flag", "flag");
//animate Flag
void animateFlag(){
if(color == 3){
pixels.setPixelColor(15, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
pixels.show();
for(int count = 1; count <= 15; ++count){
delay(75);
pixels.setPixelColor(15 - count, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
pixels.show();
}
for(int count = 16; count > 5; --count){
delay(75);
pixels.setPixelColor(15 - count, pixels.Color(flag[2][0],flag[2][1] ,flag[2][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
}
}
else if(color == 2){
for(int count = 16; count > 0; --count){
pixels.setPixelColor(15 - count, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
delay(75);
}
pixels.setPixelColor(15, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
}
else if(color == 1){
for(int count = 16; count > 0; --count){
pixels.setPixelColor(15 - count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
delay(75);
}
pixels.setPixelColor(15, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
}
}
//Show Flag
void setFlagInstantly(){
if(color == 3){
pixels.setPixelColor(15, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
for(int count = 1; count <= 15; ++count){
pixels.setPixelColor(15 - count, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
}
for(int count = 16; count > 5; --count){
pixels.setPixelColor(15 - count, pixels.Color(flag[2][0],flag[2][1] ,flag[2][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
}
pixels.show();
}
else if(color == 2){
for(int count = 16; count > 0; --count){
pixels.setPixelColor(15 - count, pixels.Color(flag[1][0],flag[1][1] ,flag[1][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
}
pixels.setPixelColor(15, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
}
else if(color == 1){
for(int count = 16; count > 0; --count){
pixels.setPixelColor(15 - count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
}
pixels.setPixelColor(15, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
}
}
//animate Winning
void animateWin(boolean blue) {
if (blue) {
for (int count = 14; count <= 30; count++) {
pixels.setPixelColor(count, pixels.Color(0, 0, 255));
pixels.show();
delay(150);
}
for (int count = 1; count <= 13; count += 2) {
pixels.setPixelColor(count, pixels.Color(0, 0, 255));
pixels.show();
delay(150);
}
}
else {
pixels.setPixelColor(15, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
for(int count = 1; count <= 15; ++count){
delay(75);
pixels.setPixelColor(15 - count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.setPixelColor(15 + count, pixels.Color(flag[0][0],flag[0][1] ,flag[0][2]));
pixels.show();
}
}
}
//animate new points
void animateLastPoint(boolean blue, int whichPoint) {
if (blue) {
for(int count = 14;count >= (whichPoint - 1); count--){
if(count == 13){
pixels.setPixelColor(14, pixels.Color(0, 150, 0));
}
else if(count != 14){
pixels.setPixelColor((count + 1), pixels.Color(0, 0, 0));
}
if((whichPoint % 2) == 1){
pixels.setPixelColor(count, pixels.Color(0, 0, 255));
}
else{
pixels.setPixelColor(count, pixels.Color(255, 255, 255));
}
pixels.show();
delay(100);
}
}
else{
for(int count = 16; count <= (30 - whichPoint + 1); count++){
if(count == 17){
pixels.setPixelColor(16, pixels.Color(0, 150, 0));
}
else if(count != 16){
pixels.setPixelColor((count - 1), pixels.Color(0, 0, 0));
}
if ((whichPoint % 2) == 1) {
pixels.setPixelColor(count, pixels.Color(255, 0, 0));
}
else {
pixels.setPixelColor(count, pixels.Color(255, 96, 0));
}
pixels.show();
delay(100);
}
}
}
//handle Led Strip update
void handleLedStipUpdate(int red, int blue, String toUpdate) {
pixels.setPixelColor(15, pixels.Color(0, 150, 0));
pixels.show();
int blue0 = blue - blueToSave;
int red0 = red - redToSave;
blueToSave = blue;
redToSave = red;
if(blueToSave > redToSave){
pixels.setPixelColor(15, pixels.Color(0, 0, 255));
}
else if(blueToSave < redToSave){
pixels.setPixelColor(15, pixels.Color(255, 0, 0));
}
else{
pixels.setPixelColor(15, pixels.Color(0, 150, 0));
}
pixels.show();
if(toUpdate.equals("blue") || toUpdate.equals("red")){
if (blue > 14) {
blue = 14;
}
if (blue0 >= 1) {
for (int count = 0; count < (blue - 1); count++) {
if ((count % 2) == 0) {
pixels.setPixelColor(count, pixels.Color(0, 0, 255));
}
else {
pixels.setPixelColor(count, pixels.Color(255, 255, 255));
}
pixels.show();
}
animateLastPoint(true, blue);
}
else {
for (int count = 0; count < blue; count++) {
if ((count % 2) == 0) {
pixels.setPixelColor(count, pixels.Color(0, 0, 255));
}
else {
pixels.setPixelColor(count, pixels.Color(255, 255, 255));
}
pixels.show();
}
}
if (blue == 13) {
pixels.setPixelColor(14, pixels.Color(40, 40, 40));
pixels.show();
}
else if (blue < 13) {
pixels.setPixelColor(14, pixels.Color(0, 150, 0));
pixels.show();
}
if (blue != 14) {
for (int count = blue; count < 14; count++) {
pixels.setPixelColor(count, pixels.Color(0, 0, 0));
pixels.show();
}
}
else {
animateWin(true);
}
if (red > 14) {
red = 14;
}
if (red0 >= 1) {
for (int count = 30; count > (30 - red + 1); count--) {
if ((count % 2) == 0) {
pixels.setPixelColor(count, pixels.Color(255, 0, 0));
}
else {
pixels.setPixelColor(count, pixels.Color(255, 96, 0));
}
pixels.show();
}
animateLastPoint(false, red);
}
else {
for (int count = 30; count > (30 - red); count--) {
if ((count % 2) == 0) {
pixels.setPixelColor(count, pixels.Color(255, 0, 0));
}
else {
pixels.setPixelColor(count, pixels.Color(255, 96, 0));
}
pixels.show();
}
}
if (red == 13) {
pixels.setPixelColor(16, pixels.Color(40, 40, 40));
pixels.show();
}
else if (red < 13) {
pixels.setPixelColor(16, pixels.Color(0, 150, 0));
pixels.show();
}
if (red != 14) {
for (int count = (30 - red); count > 16; count--) {
pixels.setPixelColor(count, pixels.Color(0, 0, 0));
pixels.show();
}
}
else {
animateWin(false);
}
}
}
bool whenMessageIncomingBlue(String value){
handleLedStipUpdate(redToSave, value.toInt(), "blue");
return true;
}
bool whenMessageIncomingRed(String value){
handleLedStipUpdate(value.toInt(), blueToSave, "red");
return true;
}
bool whenMessageIncomingReset(String value){
if(value.equals("true")){
redToSave = 0;
blueToSave = 0;
handleLedStipUpdate(redToSave, blueToSave, "blue");
handleLedStipUpdate(redToSave, blueToSave, "red");
}
return true;
}
bool whenMessageIncomingFlag(String value){
if(value == "start"){
flagIncoming = true;
}
else if(value == "shootout"){
flagIncoming = true;
shootout = true;
}
else if(flagIncoming){
if(value == "show"){
flagIncoming = false;
if(shootout){
setFlagInstantly();
}
else{
animateFlag();
}
shootout = false;
color = 0;
flag[0][0] = -1;
flag[0][1] = -1;
flag[0][2] = -1;
flag[1][0] = -1;
flag[1][1] = -1;
flag[1][2] = -1;
flag[2][0] = -1;
flag[2][1] = -1;
flag[2][2] = -1;
return true;
}
if(flag[color][0] == -1){
flag[color][0] = value.toInt();
}
else if(flag[color][1] == -1){
flag[color][1] = value.toInt();
}
else if(flag[color][2] == -1){
flag[color][2] = value.toInt();
color += 1;
}
}
return true;
}
//All up to setup!
void setup() {
flag[0][0] = -1;
flag[0][1] = -1;
flag[0][2] = -1;
flag[1][0] = -1;
flag[1][1] = -1;
flag[1][2] = -1;
flag[2][0] = -1;
flag[2][1] = -1;
flag[2][2] = -1;
nodeGoal.subscribe("blue", whenMessageIncomingBlue);
nodeGoal.subscribe("red", whenMessageIncomingRed);
nodeGoal.subscribe("reset", whenMessageIncomingReset);
nodeFlag.subscribe("flag", whenMessageIncomingFlag);
Homie.registerNode(nodeGoal);
Homie.registerNode(nodeFlag);
Homie.setFirmware("Table Soccer LedStrip Display", "1.0");
Homie.setup();
//Begins Leds
pixels.begin();
//Define Pixel Led Layout
pixels.setPixelColor(14, pixels.Color(0, 150, 0));
pixels.show();
pixels.setPixelColor(15, pixels.Color(0, 150, 0));
pixels.show();
pixels.setPixelColor(16, pixels.Color(0, 150, 0));
pixels.show();
}
//Loop every time over and over again!
void loop() {
Homie.loop();
}