Skip to content

Instantly share code, notes, and snippets.

@kmicheli
Created May 7, 2018 06:14
Show Gist options
  • Save kmicheli/8541b33fbb4e3e675209ae43318c462c to your computer and use it in GitHub Desktop.
Save kmicheli/8541b33fbb4e3e675209ae43318c462c to your computer and use it in GitHub Desktop.
#include <Adafruit_NeoPixel.h>
#include <SoftwareSerial.h>
#define reset 4 //reset button
#define CALIBRATIONTIME 20000
#define ballPixels 3 // Number of pixels in ballcount strand
#define ballPin 10 // Ball count NeoPixel LED strand is connected to this pin
Adafruit_NeoPixel ballStrip = Adafruit_NeoPixel(ballPixels, ballPin, NEO_GRB + NEO_KHZ800);
#define PIXELSPIN 9
#define NUMPIXELS 99
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIXELSPIN, NEO_GRB + NEO_KHZ800);
int ballPopReading;
int ballDet;
int autoLoad = 12;
int buttonState = 0;
int ballCounter = 0;
int ballPop = A1;
int hole = 6;
int target1;
int target2;
int target3;
int score = 0;
int a = 1; // point values for targets
int b = 3; // 10 for normal targets
// maybe 20 or 30 for hard to hit targets
unsigned long pixelsInterval=50; // the time we need to wait
unsigned long rainbowPreviousMillis=0;
uint16_t currentPixel = 0;// what pixel are we operating on
int rainbowCycles = 0;
int rainbowCycleCycles = 0;
const int softwareTx = 5;
const int softwareRx = 11;
SoftwareSerial s7s(softwareRx, softwareTx);
unsigned int counter = 0; // This variable will count up to 65k
char tempString[10]; // Will be used with sprintf to create strings
void setup()
{
pinMode(reset, INPUT_PULLUP);
pinMode(autoLoad, OUTPUT);
pinMode(hole, OUTPUT);
pinMode(target1, INPUT_PULLUP);
pinMode(target2, INPUT_PULLUP);
pinMode(target3, INPUT_PULLUP);
ballStrip.begin();
ballStrip.setBrightness(10);
ballStrip.show(); // Initialize all pixels to 'off'
currentPixel = 0;
pixels.setBrightness(10);
pixels.begin(); // This initializes the NeoPixel library.
pixels.show(); // This sends the updated pixel color to the hardware.
// Must begin s7s software serial at the correct baud rate.
s7s.begin(9600);
clearDisplay(); // Clears display, resets cursor
setBrightness(255); // High brightness
// Clear the display before jumping into loop
clearDisplay();
Serial.begin(9600);
}
void loop() {
sprintf(tempString, "%4d", score);
s7s.print(tempString);
//counter++; // Increment the counter
delay(100); // This will make the display update at 10Hz.
target1 = digitalRead(A3);
target2 = digitalRead(A4);
target3 = digitalRead(A5);
Serial.print(" 1: ");
Serial.println(target1);
Serial.print(" 2: ");
Serial.println(target2);
//Serial.print(" 3: ");
//Serial.println(target3);
//Serial.print(" hole: ");
//Serial.println(ballPopReading);
//Serial.print(" score: ");
//Serial.println(score);
//Serial.print(" tempString: ");
//Serial.println(tempString);
if(!target1 || !target2 || !target3){ // can be separate if loops for each target
score = a + score;
}
//lights blink
buttonState = digitalRead(reset);
if ((unsigned long)(millis() - rainbowPreviousMillis) >= pixelsInterval) {
rainbowPreviousMillis = millis();
rainbow();
}
if (buttonState == LOW) {
digitalWrite(autoLoad, HIGH);
uint16_t i;
for(i=0; i< pixels.numPixels(); i++) {
pixels.setPixelColor(i,100,0,0); //red
}
pixels.show();
ballCounter++;
delay(150);
}
else {
digitalWrite(autoLoad, LOW);
}
//hole code
ballPopReading = analogRead(A1);
if (ballPopReading <= 800){
digitalWrite(hole, HIGH);
score = score + b;
uint16_t i;
for(i=0; i< pixels.numPixels(); i++) {
pixels.setPixelColor(i,0,0,100); //blue
}
pixels.show();
delay(200);
}
else{
digitalWrite(hole, LOW);
}
if (ballCounter == 1) {
ballStrip.setPixelColor(0, 0, 100, 0); //green
ballStrip.setPixelColor(1, 0, 100, 0); //green
ballStrip.setPixelColor(2, 0, 100, 0); //green
ballStrip.show();
}
if (ballCounter == 2) {
ballStrip.setPixelColor(0, 0, 100, 0); //green
ballStrip.setPixelColor(1, 0, 100, 0); //green
ballStrip.setPixelColor(2, 0, 0, 0); //green
ballStrip.show();
}
if (ballCounter == 3) {
ballStrip.setPixelColor(0, 0, 100, 0); //green
ballStrip.setPixelColor(1, 0, 0, 0); //green
ballStrip.setPixelColor(2, 0, 0, 0); //green
ballStrip.show();
}
if (ballCounter == 4) {
ballStrip.setPixelColor(0, 0, 0, 0); //green
ballStrip.setPixelColor(1, 0, 0, 0); //green
ballStrip.setPixelColor(2, 0, 0, 0); //green
ballStrip.show();
ballCounter = 0;
}
// Serial.println(buttonState);
}
void rainbow() {
for(uint16_t i=0; i<pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel((i+rainbowCycles) & 255));
}
pixels.show();
rainbowCycles++;
if(rainbowCycles >= 256) rainbowCycles = 0;
}
void rainbowCycle() {
uint16_t i;
//for(j=0; j<256*5; j++) { // 5 cycles of all colors on wheel
for(i=0; i< pixels.numPixels(); i++) {
pixels.setPixelColor(i, Wheel(((i * 256 / pixels.numPixels()) + rainbowCycleCycles) & 255));
}
pixels.show();
rainbowCycleCycles++;
if(rainbowCycleCycles >= 256*5) rainbowCycleCycles = 0;
}
// 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 pixels.Color(255 - WheelPos * 3, 0, WheelPos * 3);
}
if(WheelPos < 170) {
WheelPos -= 85;
return pixels.Color(0, WheelPos * 3, 255 - WheelPos * 3);
}
WheelPos -= 170;
return pixels.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
void clearDisplay()
{
s7s.write(0x76); // Clear display command
}
// Set the displays brightness. Should receive byte with the value
// to set the brightness to
// dimmest------------->brightest
// 0--------127--------255
void setBrightness(byte value)
{
s7s.write(0x7A); // Set brightness command byte
s7s.write(value); // brightness data byte
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment