Created
June 13, 2016 10:23
-
-
Save Tormodli/bab887a3ddcd6935bd2015821466ec39 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// bibliotek for LEDs | |
#include <Adafruit_NeoPixel.h> | |
// bibliotek for kortleser | |
#include <SPI.h> | |
#include <MFRC522.h> | |
// bibliotek for I2C kommunikasjon | |
#include <Wire.h> | |
// bibliotek for Capacitive touch breakout board | |
#include "Adafruit_MPR121.h" | |
int innerLight[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}; | |
// Pattern types supported: | |
enum pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, SCANNER, FADE, STEADY, TOUCHY }; | |
// Patern directions supported: | |
enum direction { FORWARD, REVERSE }; | |
// NeoPattern Class - derived from the Adafruit_NeoPixel class | |
class NeoPatterns : public Adafruit_NeoPixel | |
{ | |
public: | |
// Member Variables: | |
pattern ActivePattern; // which pattern is running | |
direction Direction; // direction to run the pattern | |
unsigned long Interval; // milliseconds between updates | |
unsigned long lastUpdate; // last update of position | |
uint32_t Color1, Color2; // What colors are in use | |
uint16_t TotalSteps; // total number of steps in the pattern | |
uint16_t Index; // current step within the pattern | |
int Level; // Brukes til å indikere nivå på det innerste lyset | |
int TenteLys[]; // brukes for å indikere hvilke lys som skal tennes | |
void (*OnComplete)(); // Callback on completion of pattern | |
// Constructor - calls base-class constructor to initialize strip | |
NeoPatterns(uint16_t pixels, uint8_t pin, uint8_t type, void (*callback)()) | |
:Adafruit_NeoPixel(pixels, pin, type) | |
{ | |
OnComplete = callback; | |
} | |
// Update the pattern | |
void Update() | |
{ | |
if((millis() - lastUpdate) > Interval) // time to update | |
{ | |
lastUpdate = millis(); | |
switch(ActivePattern) | |
{ | |
case RAINBOW_CYCLE: | |
RainbowCycleUpdate(); | |
break; | |
case THEATER_CHASE: | |
TheaterChaseUpdate(); | |
break; | |
case COLOR_WIPE: | |
ColorWipeUpdate(); | |
break; | |
case SCANNER: | |
ScannerUpdate(); | |
break; | |
case FADE: | |
FadeUpdate(); | |
break; | |
case STEADY: | |
SteadyUpdate(); | |
break; | |
case TOUCHY: | |
TouchyUpdate(); | |
break; | |
default: | |
break; | |
} | |
} | |
} | |
// Increment the Index and reset at the end | |
void Increment() | |
{ | |
if (Direction == FORWARD) | |
{ | |
Index++; | |
if (Index >= TotalSteps) | |
{ | |
Index = 0; | |
if (OnComplete != NULL) | |
{ | |
OnComplete(); // call the comlpetion callback | |
} | |
} | |
} | |
else // Direction == REVERSE | |
{ | |
--Index; | |
if (Index <= 0) | |
{ | |
Index = TotalSteps-1; | |
if (OnComplete != NULL) | |
{ | |
OnComplete(); // call the comlpetion callback | |
} | |
} | |
} | |
} | |
// Reverse pattern direction | |
void Reverse() | |
{ | |
if (Direction == FORWARD) | |
{ | |
Direction = REVERSE; | |
Index = TotalSteps-1; | |
} | |
else | |
{ | |
Direction = FORWARD; | |
Index = 0; | |
} | |
} | |
// Initialize for a RainbowCycle | |
void RainbowCycle(uint8_t interval, direction dir = FORWARD) | |
{ | |
ActivePattern = RAINBOW_CYCLE; | |
Interval = interval; | |
TotalSteps = 255; | |
Index = 0; | |
Direction = dir; | |
} | |
// Update the Rainbow Cycle Pattern | |
void RainbowCycleUpdate() | |
{ | |
for(int i=0; i< numPixels(); i++) | |
{ | |
setPixelColor(i, Wheel(((i * 256 / numPixels()) + Index) & 255)); | |
} | |
show(); | |
Increment(); | |
} | |
// Metode for fast lys som ikke blinker. Color 1 indikerer nivå. | |
void Steady(uint32_t color1, uint32_t color2, int level) { | |
ActivePattern = STEADY; | |
Color1 = color1; | |
Color2 = color2; | |
Level = level; | |
} | |
// oppdaterer Steady pattern | |
void SteadyUpdate() { | |
for(int i=0; i< numPixels(); i++) | |
{ | |
setPixelColor(i, Color2); | |
} | |
//if (Level !=0) | |
for(int j=0; j< numPixels()/**Level)/4*/; j++) | |
{ | |
if(innerLight[j] == 1){ | |
setPixelColor(j, Color1); | |
} | |
} | |
show(); | |
} | |
// Metode for fast lys som ikke blinker. Color 1 indikerer nivå. | |
void Touchy(uint32_t color1, uint32_t color2) { | |
ActivePattern = TOUCHY; | |
Color1 = color1; | |
Color2 = color2; | |
} | |
// oppdaterer Steady pattern | |
void TouchyUpdate() { | |
for(int i=0; i< numPixels(); i++) | |
{ | |
if (TenteLys[i] == 0 ) { | |
setPixelColor(i, Color2); | |
} | |
if (TenteLys[i] == 1 ) { | |
setPixelColor(i, Color1); | |
} | |
} | |
show(); | |
} | |
// Initialize for a Theater Chase | |
void TheaterChase(uint32_t color1, uint32_t color2, uint8_t interval, direction dir = FORWARD) | |
{ | |
ActivePattern = THEATER_CHASE; | |
Interval = interval; | |
TotalSteps = numPixels(); | |
Color1 = color1; | |
Color2 = color2; | |
Index = 0; | |
Direction = dir; | |
} | |
void midGreen(){ | |
} | |
// Update the Theater Chase Pattern | |
void TheaterChaseUpdate() | |
{ | |
for(int i=0; i< numPixels(); i++) | |
{ | |
if ((i + Index) % 3 == 0) | |
{ | |
setPixelColor(i, Color1); | |
} | |
else | |
{ | |
setPixelColor(i, Color2); | |
} | |
} | |
show(); | |
Increment(); | |
} | |
// Initialize for a ColorWipe | |
void ColorWipe(uint32_t color, uint8_t interval, direction dir = FORWARD) | |
{ | |
ActivePattern = COLOR_WIPE; | |
Interval = interval; | |
TotalSteps = numPixels(); | |
Color1 = color; | |
Index = 0; | |
Direction = dir; | |
} | |
// Update the Color Wipe Pattern | |
void ColorWipeUpdate() | |
{ | |
setPixelColor(Index, Color1); | |
show(); | |
Increment(); | |
} | |
// Initialize for a SCANNNER | |
void Scanner(uint32_t color1, uint8_t interval) | |
{ | |
ActivePattern = SCANNER; | |
Interval = interval; | |
TotalSteps = (numPixels() - 1) * 2; | |
Color1 = color1; | |
Index = 0; | |
} | |
// Update the Scanner Pattern | |
void ScannerUpdate() | |
{ | |
for (int i = 0; i < numPixels(); i++) | |
{ | |
if (i == Index) // Scan Pixel to the right | |
{ | |
setPixelColor(i, Color1); | |
} | |
else if (i == TotalSteps - Index) // Scan Pixel to the left | |
{ | |
setPixelColor(i, Color1); | |
} | |
else // Fading tail | |
{ | |
setPixelColor(i, DimColor(getPixelColor(i))); | |
} | |
} | |
show(); | |
Increment(); | |
} | |
// Initialize for a Fade | |
void Fade(uint32_t color1, uint32_t color2, uint16_t steps, uint8_t interval, direction dir = FORWARD) | |
{ | |
ActivePattern = FADE; | |
Interval = interval; | |
TotalSteps = steps; | |
Color1 = color1; | |
Color2 = color2; | |
Index = 0; | |
Direction = dir; | |
} | |
// Update the Fade Pattern | |
void FadeUpdate() | |
{ | |
// Calculate linear interpolation between Color1 and Color2 | |
// Optimise order of operations to minimize truncation error | |
uint8_t red = ((Red(Color1) * (TotalSteps - Index)) + (Red(Color2) * Index)) / TotalSteps; | |
uint8_t green = ((GREEN(Color1) * (TotalSteps - Index)) + (GREEN(Color2) * Index)) / TotalSteps; | |
uint8_t blue = ((BLUE(Color1) * (TotalSteps - Index)) + (BLUE(Color2) * Index)) / TotalSteps; | |
ColorSet(Color(red, green, blue)); | |
show(); | |
Increment(); | |
} | |
// Calculate 50% dimmed version of a color (used by ScannerUpdate) | |
uint32_t DimColor(uint32_t color) | |
{ | |
// Shift R, G and B components one bit to the right | |
uint32_t dimColor = Color(Red(color) >> 1, GREEN(color) >> 1, BLUE(color) >> 1); | |
return dimColor; | |
} | |
// Set all pixels to a color (synchronously) | |
void ColorSet(uint32_t color) | |
{ | |
for (int i = 0; i < numPixels(); i++) | |
{ | |
setPixelColor(i, color); | |
} | |
show(); | |
} | |
// Returns the Red component of a 32-bit color | |
uint8_t Red(uint32_t color) | |
{ | |
return (color >> 16) & 0xFF; | |
} | |
// Returns the GREEN component of a 32-bit color | |
uint8_t GREEN(uint32_t color) | |
{ | |
return (color >> 8) & 0xFF; | |
} | |
// Returns the BLUE component of a 32-bit color | |
uint8_t BLUE(uint32_t color) | |
{ | |
return color & 0xFF; | |
} | |
// Input a value 0 to 255 to get a color value. | |
// The colours are a transition r - g - b - back to r. | |
uint32_t Wheel(byte WheelPos) | |
{ | |
WheelPos = 255 - WheelPos; | |
if(WheelPos < 85) | |
{ | |
return Color(255 - WheelPos * 3, 0, WheelPos * 3); | |
} | |
else if(WheelPos < 170) | |
{ | |
WheelPos -= 85; | |
return Color(0, WheelPos * 3, 255 - WheelPos * 3); | |
} | |
else | |
{ | |
WheelPos -= 170; | |
return Color(WheelPos * 3, 255 - WheelPos * 3, 0); | |
} | |
} | |
}; | |
void InnerComplete(); | |
void MiddleComplete(); | |
void OuterComplete(); | |
// Inner = 1 x neopixel ring | |
// Middle = 3 x neopixel jewel | |
// Outer = 4 x single neopixels | |
NeoPatterns Inner(16, 4, NEO_GRB + NEO_KHZ800, &InnerComplete); | |
NeoPatterns Middle(21, 6, NEO_GRB + NEO_KHZ800, &MiddleComplete); | |
NeoPatterns Outer(4, 5, NEO_GRB + NEO_KHZ800, &OuterComplete); | |
// capacitive touch sensor | |
// You can have up to 4 on one i2c bus but one is enough for testing! | |
Adafruit_MPR121 cap = Adafruit_MPR121(); | |
// kortleser | |
#define SS_PIN 10 | |
#define RST_PIN 9 | |
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class | |
uint16_t lasttouched = 0; | |
uint16_t currtouched = 0; | |
int touchPad = 0; | |
long cardCooldown = 0; | |
long COOLDOWNTIME = 5000; | |
long leseInterval = 10000; // hvor lang tid man har på å aktivere alle touchpadene etter et kort er lest | |
int n = 0; // Hvilket nivå man er på (0-4) | |
int touch[12]={0,0,0,0,0,0,0,0,0,0,0,0}; // hvilke touchpader som er aktivert (5,7,9,11) | |
int antallTouch = 0; // antall touch registrert samtidig (0-4) | |
int kortLest = 0; // hvilket kort er lest (1-4) | |
int kortStatus = 0; // 0 = ingen kort lest, 1 = nytt kort lest, 2 = kort lest tidligere | |
int antallObjekter[3] = {0,0,0}; // antall objekter registrert | |
long forrigeMillis = 0; // for Wire.write() | |
long beskjedIntervall = 50; // for Wire.write(), hvor ofte beskjed skal sendes til Arduino #2 | |
static int ORANGE[] = {70,62,252,19}; | |
static int BLUE[] = {0,21,96,157}; | |
static int GREEN[] = {236,156,171,133}; | |
static int KORT4[] = {206,86,97,31}; | |
static int KORT_TID[] = {14,188,237,32}; | |
static int KORT_UTEN_TID[] = {102,35,252,35}; | |
static int LIKE_INTERVAL = 0; | |
static long TOUCHTOACTIVATE = 3000; | |
enum kort {_ORANGE, _BLUE, _GREEN, _UKJENT ,_KORT4 , _TID , _UTEN_TID}; | |
kort lastRead; | |
//boolean chooseGame = true; | |
enum spillmodus {TIMED,UNTIMED}; | |
spillmodus spill; | |
long timeToActivate = 0; // teller for tiden etter kort er skannet | |
boolean startActivation = false; // for tiden etter et kort er skannet | |
long touchTimer; | |
boolean orangeFound = false; | |
boolean blueFound = false; | |
boolean greenFound = false; | |
boolean kort4Found = false; | |
boolean playing = false; | |
boolean won = false; | |
boolean enoughTouch; | |
int numberFound; | |
boolean touching = false; | |
int sangNR; | |
int byttMusikk; | |
int songPlaythrough; | |
static int NUMBEROFPLAYTHROUGHS = 6; | |
static long SONGDURATION = 58000; | |
int currentSong; | |
long songTime; | |
boolean partFound = false; | |
int innerLevel = 0; | |
boolean failed = false; | |
long failedTimer = 0; | |
//1.mp3 victory | |
//2.mp3 alarm - dårlig tid | |
//3.mp3 basic sang | |
//4.mp3 finne en ekstra del | |
//5.mp3 enhanced sang | |
//6.mp3 electro fail | |
//7.mp3 wah wah waaah | |
// Initialize everything and prepare to start | |
void setup() | |
{ | |
// for kortleser | |
SPI.begin(); // Init SPI bus | |
rfid.PCD_Init(); // Init MFRC522 | |
// Start kode for annet enn neopixels | |
Serial.begin(9600); | |
Wire.begin(); // join i2c bus (address optional for master) | |
// sjekker om cap sensor er tilstede | |
if (!cap.begin(0x5A)) { | |
Serial.println("MPR121 not found, check wiring?"); | |
while (1); | |
} | |
// slutt kode for annet enn neopixel | |
// Initialize all the pixelStrips | |
Inner.begin(); | |
Middle.begin(); | |
Outer.begin(); | |
// Kick off a pattern | |
// Inner.TheaterChase(Inner.Color(255,255,0), Inner.Color(0,0,50), 100); | |
// Middle.TheaterChase(Middle.Color(255,50,0), Middle.Color(0,0,50), 100); | |
//Middle.Color1 = Outer.Color1; | |
// Outer.Scanner(Inner.Color(255,0,0), 55); | |
Inner.Steady(Inner.Color(0,0,50), Inner.Color(0,0,0), 0); | |
Middle.Fade(Middle.Color(0,0,0), Middle.Color(20,0,0), 200, 20, FORWARD); | |
//Outer.Fade(Outer.Color(0,0,50), Outer.Color(0,0,0), 50, 80, FORWARD); | |
// Outer.Fade(Outer.Color(0,100,100), Outer.Color(100,100,0), 50, 50, FORWARD); | |
Outer.setPixelColor(1,Outer.Color(0,0,100)); | |
Outer.show(); | |
// Outer.update(); | |
} | |
// Main loop | |
void loop() | |
{ | |
/* | |
if (kortLest == 1 || kortLest == 2 || kortLest == 3) { | |
if (songTimer == true) { | |
timeCounter = millis(); | |
songTimer = false; | |
} | |
// kort skannet tidligere | |
if ((kortLest == 1 && antallObjekter[0] == 1) || (kortLest == 2 && antallObjekter[1] == 1) || (kortLest == 3 && antallObjekter[2] == 1)) { | |
// currentColor = strip.Color(50,15,0); | |
// respons = 2; | |
// | |
kortStatus = 2; | |
Middle.Color2 = Middle.Color(50,20,0); | |
} | |
else { | |
// nytt kort skannet | |
// currentColor = strip.Color(0,50,0); | |
// respons = 1; | |
kortStatus = 1; | |
// Middle.Fade(Middle.Color(0,0,0), Middle.Color(0,50,0), 200, 20, FORWARD); | |
Middle.Color2 = Middle.Color(0,100,0); | |
} | |
if (millis() - timeCounter > leseInterval) { | |
timeCounter = 0; | |
kortLest = 0; | |
kortStatus = 0; | |
} | |
} | |
else { | |
// currentColor = strip.Color(50,0,0); | |
// Middle.Fade(Middle.Color(0,0,0), Middle.Color(50,0,0), 200, 20, FORWARD); | |
Middle.Color2 = Middle.Color(50,0,0); | |
}*/ | |
if (failed && millis()-failedTimer > 4000){ | |
songTime = millis(); | |
sendMelding(currentSong,0); | |
failed = false; | |
} | |
antallTouch = 0; | |
currtouched = cap.touched(); | |
for (uint8_t i=0; i<12; i++) { | |
// it if *is* touched and *wasnt* touched before, alert! | |
if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) { | |
//Serial.print(i); Serial.println(" touched"); | |
touch[i] = 1; | |
} | |
// if it *was* touched and now *isnt*, alert! | |
if (!(currtouched & _BV(i)) && (lasttouched & _BV(i)) ) { | |
//Serial.print(i); Serial.println(" released"); | |
touch[i] = 0; | |
} | |
} | |
// sjekker antall touch og lyser opp LEDs | |
for (int k = 0; k<12;k++) { | |
// Serial.print(antTouch[k]); | |
// Serial.print(","); | |
if (touch[k] == 1) { | |
antallTouch++; | |
//Serial.println(k); | |
} | |
} | |
// lyser opp LED for hver touch-flate som blir berørt | |
if (touch[7] == 1) { | |
Outer.setPixelColor(0,Outer.Color(0,0,100)); | |
} | |
if (touch[11] == 1) { | |
Outer.setPixelColor(1,Outer.Color(0,0,100)); | |
} | |
if (touch[9] == 1) { | |
Outer.setPixelColor(2,Outer.Color(0,0,100)); | |
} | |
if (touch[5] == 1) { | |
Outer.setPixelColor(3,Outer.Color(0,0,100)); | |
} | |
if (touch[7] == 0) { | |
Outer.setPixelColor(0,Outer.Color(50,100,0)); | |
} | |
if (touch[11] == 0) { | |
Outer.setPixelColor(1,Outer.Color(50,100,0)); | |
} | |
if (touch[9] == 0) { | |
Outer.setPixelColor(2,Outer.Color(50,100,0)); | |
} | |
if (touch[5] == 0) { | |
Outer.setPixelColor(3,Outer.Color(50,100,0)); | |
} | |
Outer.show(); | |
/* //Middle.ActivePattern = RAINBOW_CYCLE; | |
//Middle.ActivePattern = STEADY; | |
Middle.ActivePattern = FADE; | |
Middle.Interval = 5; | |
Middle.TotalSteps = 10; | |
Middle.Color1 = Middle.Color(0,30,0); | |
Middle.Color2 = Middle.Color(0,60,0); | |
//Middle.Fade(Middle.Color(30,0,0), Middle.Color(0,0,60), 100, 0, FORWARD); | |
*/ | |
if(!startActivation){ | |
Middle.ActivePattern = FADE; | |
Middle.Interval = 10; | |
Middle.TotalSteps = 50; | |
Middle.Color1 = Middle.Color(30,5,5); | |
Middle.Color2 = Middle.Color(20,0,0); | |
}else if(lastRead == _GREEN){ | |
Middle.ActivePattern = FADE; | |
if(spill == TIMED){ | |
Middle.TotalSteps = 10; | |
}else{ | |
Middle.TotalSteps = 25; | |
} | |
Middle.Interval = 5; | |
Middle.Color1 = Middle.Color(0,30,0); | |
Middle.Color2 = Middle.Color(0,60,0); | |
}else if(lastRead == _BLUE){ | |
Middle.ActivePattern = FADE; | |
Middle.Interval = 5; | |
if(spill == TIMED){ | |
Middle.TotalSteps = 10; | |
}else{ | |
Middle.TotalSteps = 25; | |
} | |
Middle.Color1 = Middle.Color(0,0,30); | |
Middle.Color2 = Middle.Color(0,0,60); | |
}else if(lastRead == _ORANGE){ | |
Middle.ActivePattern = FADE; | |
Middle.Interval = 5; | |
if(spill == TIMED){ | |
Middle.TotalSteps = 10; | |
}else{ | |
Middle.TotalSteps = 25; | |
} | |
Middle.Color1 = Middle.Color(60,40,0); | |
Middle.Color2 = Middle.Color(30,17,0); | |
}else if(lastRead == _KORT4){ | |
Middle.ActivePattern = FADE; | |
Middle.Interval = 5; | |
if(spill == TIMED){ | |
Middle.TotalSteps = 10; | |
}else{ | |
Middle.TotalSteps = 25; | |
} | |
Middle.Color1 = Middle.Color(5,7,15); | |
Middle.Color2 = Middle.Color(15,7,5); | |
} | |
//debug, må erstattes av rett antall touch!!! | |
touching = false; | |
if(antallTouch == 4){ | |
//enoughTouch = true; | |
touching = true; | |
}else{ | |
enoughTouch = false; | |
} | |
if(!touching){ | |
touchTimer = millis(); | |
} | |
if (startActivation && ((millis()-timeToActivate) > leseInterval)){ | |
startActivation = false; | |
failedActivation(); | |
} | |
if (startActivation && touching && ((millis() - touchTimer) < TOUCHTOACTIVATE)){ | |
ongoingActivation(); | |
} | |
if (startActivation && touching && ((millis() - touchTimer) > TOUCHTOACTIVATE)){ | |
startActivation = false; | |
completedActivation(); | |
enoughTouch = true; | |
} | |
if (playing && enoughTouch){ | |
if (lastRead == _ORANGE && !orangeFound){ | |
orangeFound = true; | |
sendMelding(4,0); | |
songTime = millis(); | |
partFound = true; | |
Serial.println("orange found!"); | |
}else if (lastRead == _BLUE && !blueFound){ | |
blueFound = true; | |
sendMelding(4,0); | |
partFound = true; | |
songTime = millis(); | |
Serial.println("blue found!"); | |
}else if (lastRead == _GREEN && !greenFound){ | |
greenFound = true; | |
sendMelding(4,0); | |
partFound = true; | |
songTime = millis(); | |
Serial.println("green found!"); | |
}else if (lastRead == _KORT4 && !kort4Found){ | |
kort4Found = true; | |
sendMelding(4,0); | |
partFound = true; | |
songTime = millis(); | |
Serial.println("kort4 found!"); | |
} | |
} | |
if (playing && partFound && millis()-songTime > 3000){ | |
songTime = millis(); | |
sendMelding(currentSong,0); | |
partFound = false; | |
} | |
numberFound = 0; | |
if (orangeFound){ | |
numberFound ++; | |
} | |
if (blueFound){ | |
numberFound ++; | |
} | |
if (greenFound){ | |
numberFound ++; | |
} | |
if (kort4Found){ | |
numberFound++; | |
} | |
if (numberFound == 4){ | |
won = true; | |
//do win stuff | |
} | |
if(!startActivation){ | |
Inner.Level = numberFound; | |
innerToLevel(); | |
} | |
if(won && playing){ | |
delay(1500); | |
Serial.println("VICTORY"); | |
sendMelding(1,1); | |
playing = false; | |
} | |
// Update the rings. | |
Inner.Update(); | |
Outer.Update(); | |
Middle.Update(); | |
// nullstiller antall touch | |
for (int l = 0; l<12;l++) { | |
touch[l] = 0; | |
} | |
antallTouch = 0; | |
if (won ) { | |
Inner.ActivePattern = RAINBOW_CYCLE; | |
Middle.ActivePattern = RAINBOW_CYCLE; | |
Outer.ActivePattern = RAINBOW_CYCLE; | |
} | |
// Serial.println(millis()- songTime); | |
if(spill == UNTIMED && playing && (millis()- songTime) > SONGDURATION){ | |
sendMelding(currentSong,0); | |
songTime = millis(); | |
} | |
if(playing && spill == TIMED){ | |
if ( (millis()- songTime) > SONGDURATION && songPlaythrough < NUMBEROFPLAYTHROUGHS){ | |
sendMelding(currentSong,0); | |
songTime = millis(); | |
songPlaythrough++; | |
}else if (millis() - songTime > SONGDURATION && songPlaythrough > NUMBEROFPLAYTHROUGHS){ | |
sendMelding(7,0); | |
playing = false; | |
// gameOver(); | |
} | |
} | |
//DANGERZONE***!!!!!****DANGERZONE etter dette punktet kommer du bare ved nytt kort | |
if ((millis() - cardCooldown) < COOLDOWNTIME){ | |
return; | |
} | |
// kode for kortleser | |
// Look for new cards | |
if ( ! rfid.PICC_IsNewCardPresent()){ | |
return; | |
} | |
// Verify if the NUID has been read | |
if ( ! rfid.PICC_ReadCardSerial()){ | |
return; | |
} | |
Serial.println("0:"); | |
Serial.println(rfid.uid.uidByte[0]); | |
Serial.println("1:"); | |
Serial.println(rfid.uid.uidByte[1]); | |
Serial.println("2:"); | |
Serial.println(rfid.uid.uidByte[2]); | |
Serial.println("3:"); | |
Serial.println(rfid.uid.uidByte[3]); | |
lastRead = SjekkKort(rfid.uid.uidByte); | |
if (lastRead == _TID){ | |
startGame(true); | |
}else if (lastRead == _UTEN_TID){ | |
startGame(false); | |
}else{ | |
timeToActivate = millis(); // teller for tiden etter kort er skannet | |
startActivation = true; // for tiden etter et kort er skannet | |
} | |
if (lastRead == _BLUE){ | |
Serial.println("blue"); | |
} | |
if (lastRead == _ORANGE){ | |
Serial.println("orange"); | |
} | |
if (lastRead == _GREEN){ | |
Serial.println("green"); | |
} | |
if (lastRead == _KORT4){ | |
Serial.println("kort4"); | |
} | |
if (lastRead == _UKJENT){ | |
Serial.println("nytt kort??"); | |
} | |
cardCooldown = millis(); | |
/* | |
Serial.println("0:"); | |
Serial.println(rfid.uid.uidByte[0]); | |
Serial.println("1:"); | |
Serial.println(rfid.uid.uidByte[1]); | |
Serial.println("2:"); | |
Serial.println(rfid.uid.uidByte[2]); | |
Serial.println("3:"); | |
Serial.println(rfid.uid.uidByte[3]); | |
*/ | |
/* | |
if (rfid.uid.uidByte[0] == 0 || | |
rfid.uid.uidByte[1] == 21 || | |
rfid.uid.uidByte[2] == 96 || | |
rfid.uid.uidByte[3] == 157 ) { | |
kortLest = 1; | |
songTimer = true; | |
} | |
else if (rfid.uid.uidByte[0] == 70 || | |
rfid.uid.uidByte[1] == 62 || | |
rfid.uid.uidByte[2] == 252 || | |
rfid.uid.uidByte[3] == 19 ) { | |
kortLest = 2; | |
songTimer = true; | |
} | |
else if (rfid.uid.uidByte[0] == 236 || | |
rfid.uid.uidByte[1] == 156 || | |
rfid.uid.uidByte[2] == 171 || | |
rfid.uid.uidByte[3] == 133 ) { | |
kortLest = 3; | |
songTimer = true; | |
} | |
*/ | |
//else Serial.println(F("Feil kort...")); | |
// Halt PICC | |
rfid.PICC_HaltA(); | |
} | |
//returnerer true hvis meldingen er sendt, false ellers | |
boolean sendMelding(int sang, int bytt, int touch){ | |
// skriver til Arduino #2 hvis det er gått mer en 50 millisek siden forrige beskjed | |
if ((unsigned long)(millis() - forrigeMillis) >= beskjedIntervall) { | |
int beskjed = 16*touch + 2*sang + bytt; | |
forrigeMillis = millis(); | |
Wire.beginTransmission(8); | |
Wire.write(beskjed); // sends one byte | |
Wire.endTransmission(); // stop transmitting | |
Serial.println("sender beskjed"); | |
Serial.println(beskjed); | |
return true; | |
}else{ | |
Serial.println("not sent"); | |
return false; | |
} | |
} | |
long lightSet = 0; | |
int lastLit = 0; | |
void failedActivation(){ | |
Serial.println("Mislykket aktivering"); | |
failed = true; | |
failedTimer = millis(); | |
if (spill == TIMED){ | |
sendMelding(6,1); | |
}else{ | |
sendMelding(7,1); | |
} | |
lightSet = 0; | |
lastLit = 0; | |
} | |
void innerToLevel(){ | |
for(int i = 0; i < 16 ; i++){ | |
innerLight[i] = 0; | |
if (Inner.Level*4 > i){ | |
innerLight[i] = 1; | |
} | |
} | |
} | |
void ongoingActivation(){ | |
if (lastLit < 4 && (millis() - lightSet) > 1000){ | |
innerLight[Inner.Level*4 + lastLit] = 1; | |
lastLit++; | |
lightSet = millis(); | |
} | |
Serial.println("pågående aktivering"); | |
Serial.println(Inner.Level); | |
} | |
void completedActivation(){ | |
Serial.println("vellykket aktivering"); | |
Inner.Level++; | |
if(Inner.Level == 4){ | |
won = true; | |
} | |
innerToLevel(); | |
lightSet = 0; | |
lastLit = 0; | |
} | |
//returnerer true hvis meldingen er sendt, false ellers denne kan brukes hvis touch er irrelevant :) | |
boolean sendMelding(int sangNR, int bytt){ | |
// skriver til Arduino #2 hvis det er gått mer en 50 millisek siden forrige beskjed | |
if ((unsigned long)(millis() - forrigeMillis) >= beskjedIntervall) { | |
int beskjed = 2*sangNR + bytt; | |
forrigeMillis = millis(); | |
Wire.beginTransmission(8); | |
Wire.write(beskjed); // sends one byte | |
Wire.endTransmission(); // stop transmitting | |
Serial.println("sender beskjed"); | |
Serial.println(beskjed); | |
return true; | |
}else{ | |
Serial.println("not sent"); | |
return false; | |
} | |
} | |
void startGame(boolean timed){ | |
if(timed){spill = TIMED;} | |
else{spill = UNTIMED;} | |
for (int i = 0; i < 16; i++){ | |
innerLight[i] = 0; | |
} | |
failed = false; | |
forrigeMillis = 0; | |
orangeFound = false; | |
blueFound = false; | |
greenFound = false; | |
kort4Found = false; | |
playing = true; | |
numberFound = 0; | |
songTime = millis(); | |
lastRead = _UKJENT; | |
Inner.Level = 0; | |
for (int i = 0; i < 4 ; i++){ | |
Outer.setPixelColor(i,Outer.Color(0,0,100)); | |
} | |
Inner.ActivePattern = STEADY; | |
innerToLevel(); | |
Outer.ActivePattern = TOUCHY; | |
Middle.ActivePattern = FADE; | |
won = false; | |
if (timed){ | |
currentSong = 5; | |
}else{ | |
currentSong = 3; | |
} | |
songPlaythrough = 0; | |
sendMelding(currentSong,0); | |
//kortStatus = 1; | |
} | |
//to-do game over | |
void gameOver(){ | |
} | |
kort SjekkKort(byte RFID[]){ | |
for (int i = 0 ; i < 4; i++){ | |
if (RFID[i] != ORANGE[i]){ | |
break; | |
} | |
if (i >= LIKE_INTERVAL){ | |
return _ORANGE; | |
} | |
} | |
for (int i = 0 ; i < 4; i++){ | |
if (RFID[i] != BLUE[i]){ | |
break; | |
} | |
if (i > LIKE_INTERVAL){ | |
return _BLUE; | |
} | |
} | |
for (int i = 0 ; i < 4; i++){ | |
if (RFID[i] != GREEN[i]){ | |
break; | |
} | |
if (i > LIKE_INTERVAL){ | |
return _GREEN; | |
} | |
} | |
for (int i = 0 ; i < 4; i++){ | |
if (RFID[i] != KORT4[i]){ | |
break; | |
} | |
if (i > LIKE_INTERVAL){ | |
return _KORT4; | |
} | |
} | |
for (int i = 0 ; i < 4; i++){ | |
if (RFID[i] != KORT_TID[i]){ | |
break; | |
} | |
if (i > LIKE_INTERVAL){ | |
return _TID; | |
} | |
} | |
for (int i = 0 ; i < 4; i++){ | |
if (RFID[i] != KORT_UTEN_TID[i]){ | |
break; | |
} | |
if (i > LIKE_INTERVAL){ | |
return _UTEN_TID; | |
} | |
} | |
return _UKJENT; | |
} | |
//------------------------------------------------------------ | |
//Completion Routines - get called on completion of a pattern | |
//------------------------------------------------------------ | |
// Ring1 Completion Callback | |
void InnerComplete(){ | |
Inner.Reverse(); | |
} | |
// Ring 2 Completion Callback | |
void MiddleComplete(){ | |
Middle.Reverse(); | |
} | |
// Stick Completion Callback | |
void OuterComplete(){ | |
Outer.Reverse(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment