Created
December 11, 2020 08:15
-
-
Save Hackin7/03133c4274d6e8cbf813ec797aa61494 to your computer and use it in GitHub Desktop.
For WS2812 LED Matrix. TechPals Season 2 Footpress
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
#include <Adafruit_NeoPixel.h> | |
#ifdef __AVR__ | |
#include <avr/power.h> // Required for 16 MHz Adafruit Trinket | |
#endif | |
#define PIN 0 | |
#define LEDS 60 | |
#define LED_WIDTH 6 | |
#define LED_HEIGHT 10 | |
Adafruit_NeoPixel pixels(LEDS, PIN, NEO_GRB + NEO_KHZ800); | |
const int buttonA = 5; | |
#define PINGPIN 2 // Trigger Pin of Ultrasonic Sensor | |
#define ECHOPIN 1 // Echo Pin of Ultrasonic Sensor | |
void setup() { | |
#if defined(__AVR_ATtiny85__) && (F_CPU == 16000000) | |
clock_prescale_set(clock_div_1); | |
#endif | |
pixels.begin(); // INITIALIZE NeoPixel strip object (REQUIRED) | |
pixels.clear(); | |
pixels.show(); | |
Serial.begin(9600); // Starting Serial Terminal | |
pinMode(buttonA, INPUT); | |
pinMode(PINGPIN, OUTPUT); | |
pinMode(ECHOPIN, INPUT); | |
} | |
#define NUMCOLOURS 5 | |
int colours[NUMCOLOURS][3] = { | |
{0,0,0}, //blank | |
{255,0,0}, //red | |
{0,255,0}, //green | |
{255,255,0}, //yellow | |
{139,69,19}//brown | |
}; | |
void showImage(int image[]){ | |
for (int i=0;i<LEDS;i++){ | |
for (int k=0;k<NUMCOLOURS;k++){ | |
if (image[i] == k){ | |
pixels.setPixelColor(i, pixels.Color(colours[k][0], | |
colours[k][1], | |
colours[k][2])); | |
} | |
} | |
} | |
pixels.show(); | |
} | |
int counter = 0; | |
int prevcmcounter = 0;; | |
bool triggered(){ | |
digitalWrite(PINGPIN, LOW); | |
delayMicroseconds(2); | |
digitalWrite(PINGPIN, HIGH); | |
delayMicroseconds(10); | |
digitalWrite(PINGPIN, LOW); | |
long t1 = micros(); | |
while (digitalRead(ECHOPIN) == 0){} | |
while (digitalRead(ECHOPIN) == 1){} | |
long t2 = micros(); | |
long duration = (t2-t1); //To Microseconds | |
//duration = pulseIn(ECHOPIN, HIGH); | |
long cm = (duration / 29 )/ 2 -10; | |
Serial.print(cm); | |
Serial.print("cm counter:"); | |
Serial.print(counter); | |
Serial.print(" Button:"); | |
Serial.print(!digitalRead(buttonA)); | |
Serial.print(" prevcmcounter:"); | |
Serial.print(prevcmcounter); | |
Serial.print(" "); | |
if (cm > 1000){ | |
return prevcmcounter>5;//previous value | |
} | |
if (cm >0 && cm < 20){ | |
prevcmcounter++; | |
} | |
else if (cm > 0 && prevcmcounter >0){ | |
prevcmcounter = 0; | |
//delay(500);//debounce | |
return true; | |
} | |
if (prevcmcounter >= 20){ | |
//prevcmcounter = 30; | |
return true; | |
} | |
return !digitalRead(buttonA); | |
} | |
bool prevState = false; | |
bool latch(){ | |
bool currentState = triggered(); | |
Serial.println(currentState); | |
if (currentState && currentState != prevState){ | |
prevState = currentState; | |
//delay(500); //Debounce | |
return true; | |
} | |
if (currentState != prevState){ | |
prevState = currentState; | |
} | |
return false; | |
} | |
#define DIFFICULTY 5 | |
#define NUMIMAGES 6 | |
int images[NUMIMAGES][60] = { | |
{0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0}, | |
{ | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2 | |
}, | |
{ | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,4,0,0, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2 | |
}, | |
{ | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,1,0,0, | |
0,0,0,4,0,0, | |
0,0,0,4,0,0, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2 | |
}, | |
{ | |
0,0,0,0,0,0, | |
0,0,1,1,0,0, | |
0,1,3,3,1,0, | |
0,1,3,3,1,0, | |
0,0,1,1,0,0, | |
0,0,0,4,0,0, | |
0,0,0,4,0,0, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2, | |
2,2,2,2,2,2 | |
} | |
}; | |
//int counter = 0; | |
int progress = 0; | |
void counting(){ | |
if (latch()){ | |
counter++; | |
resetTime(); | |
} | |
if (counter > (NUMIMAGES-1-1)*DIFFICULTY+1){ | |
counter = 0; | |
} | |
for (int i=0;i<NUMIMAGES;i++){ | |
if (counter == i*DIFFICULTY){ | |
progress = i; | |
showImage(images[i]); | |
} | |
} | |
} | |
long prevTime = millis(); | |
void resetTime(){ | |
prevTime = millis(); | |
} | |
/* | |
int jia[]= { | |
0,0,0,0,1,0,0,0, | |
0,0,0,1,0,0,0,0, | |
1,1,1,1,0,0,0,0, | |
0,0,1,1,0,1,1,1, | |
0,1,0,1,0,1,0,1, | |
1,0,0,1,0,1,1,1, | |
0,0,1,1,0,0,0,0, | |
0,0,0,0,0,0,0,0 | |
}; | |
int you[] = { | |
0,1,0,0,0,1,0,0, | |
0,0,1,0,0,1,0,0, | |
1,0,0,1,1,1,1,1, | |
0,1,0,1,0,1,0,1, | |
0,0,0,1,1,1,1,1, | |
0,1,0,1,0,1,0,1, | |
1,0,0,1,1,1,1,1, | |
0,0,0,0,0,0,0,0 | |
};*/ | |
int heart[] = { | |
0,0,0,0,0,0, | |
0,1,0,0,1,0, | |
1,1,1,1,1,1, | |
1,1,1,1,1,1, | |
1,1,1,1,1,1, | |
0,1,1,1,1,0, | |
0,1,1,1,1,0, | |
0,0,1,1,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0 | |
}; | |
int jiayou[4][60] = { | |
{0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0 | |
}, | |
{ 0,0,0,0,0,0, | |
0,0,0,0,1,0, | |
0,0,0,1,0,0, | |
1,1,1,1,0,0, | |
0,0,1,1,0,1, | |
0,1,0,1,0,1, | |
1,0,0,1,0,1, | |
0,0,1,1,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0 | |
}, | |
{ | |
0,0,0,0,0,0, | |
0,0,0,0,1,0, | |
0,0,0,0,0,1, | |
0,0,0,1,0,0, | |
1,1,0,0,1,0, | |
0,1,0,0,0,0, | |
1,1,0,0,1,0, | |
0,0,0,1,0,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0 | |
}, | |
{ | |
0,0,0,0,0,0, | |
0,0,1,0,0,0, | |
0,0,1,0,0,0, | |
1,1,1,1,1,0, | |
1,0,1,0,1,0, | |
1,1,1,1,1,0, | |
1,0,1,0,1,0, | |
1,1,1,1,1,0, | |
0,0,0,0,0,0, | |
0,0,0,0,0,0 | |
} | |
}; | |
void scrolling(int image1[], int image2[],int start){ | |
int newimage[LEDS]; | |
//int scrollprogress = 7; | |
for (int scrollprogress=start;scrollprogress<=LED_WIDTH;scrollprogress++){ | |
for (int j=0;j<LEDS;j+=LED_WIDTH){//rows | |
for (int i=0;i<LED_WIDTH;i++){ | |
if (j+i > LEDS)break; | |
if (i+scrollprogress >= LED_WIDTH){ | |
newimage[j+i] = image2[j+i+scrollprogress-LED_WIDTH]; | |
} | |
else{ | |
newimage[j+i] = image1[j+i+scrollprogress]; | |
} | |
} | |
} | |
showImage(newimage); | |
delay(200); | |
} | |
} | |
#define INTERVAL 60 | |
void encouragement(){ | |
if (millis()-prevTime >= INTERVAL*1000 && | |
counter < (NUMIMAGES-1-1)*DIFFICULTY){ | |
/* | |
showImage(jia); | |
delay(1000); | |
showImage(you); | |
*/ | |
//scrolling(jia,you,0); | |
scrolling(jiayou[0], jiayou[1],0); | |
scrolling(jiayou[1], jiayou[2],1); | |
scrolling(jiayou[2], jiayou[3],1); | |
delay(1000); | |
showImage(heart); | |
delay(1000); | |
resetTime(); | |
showImage(images[progress]); | |
} | |
} | |
void loop() { | |
counting(); | |
encouragement(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment