Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Arduino controlled LED Pong
- Designed and created by Schuyler Sowa
- http://www.instructables.com/member/Chip+Fixes/
- */
- //You need to install these libraries on your computer
- #include "LPD8806.h"
- #include "SPI.h"
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- //Scoreboard shift registers
- #define data1 5 //14
- #define clock1 7 //11
- #define latch1 6 //12
- //hex values of scoreboard numbers (0-9)
- byte digitOne[10]= {0x6F, 0x09, 0x73, 0x3B, 0x1D, 0x3E, 0x7C, 0x0B, 0x7F, 0x1F}; //Player 1 numbers
- byte digitTwo[10]= {0x6F, 0x09, 0x73, 0x3B, 0x1D, 0x3E, 0x7C, 0x0B, 0x7F, 0x1F}; //Player 2 numbers
- //Player 1 shift register
- #define data2 8
- #define clock2 9
- #define latch2 10
- //Player 2 shift register
- #define data3 11
- #define clock3 12
- #define latch3 13
- #define player1 1 //Player 1 push button
- #define player2 2 //Player 2 push button
- #define pot A2 //Adjusts speed of 'ball'
- int nLEDs = 30; //Number of LEDs
- int dataPin = 3; //LED Strip
- int clockPin = 4; //LED Strip
- LPD8806 strip = LPD8806(32, dataPin, clockPin);
- char turn = 'A';
- char flag = 'N';
- char cheat = 'N';
- int i = 0;
- int j = 0;
- byte player1Points = 0;
- byte player2Points = 0;
- int score;
- //LCD
- //Wire SDA to A4 and SCL to A5
- LiquidCrystal_I2C lcd(0x20,16,2); //set the LCD address to 0x20 for a 16 chars and 2 line display
- void setup()
- {
- lcd.init(); //initialize lcd
- lcd.backlight(); //turn on backlight
- pinMode(latch1, OUTPUT);
- pinMode(clock1, OUTPUT);
- pinMode(data1, OUTPUT);
- pinMode(latch2, OUTPUT);
- pinMode(clock2, OUTPUT);
- pinMode(data2, OUTPUT);
- pinMode(latch3, OUTPUT);
- pinMode(clock3, OUTPUT);
- pinMode(data3, OUTPUT);
- //Sets scoreboards to '0'
- digitalWrite(latch1, LOW);
- shiftOut(data1, clock1, MSBFIRST, ~digitOne[0]);
- shiftOut(data1, clock1, MSBFIRST, ~digitTwo[0]);
- digitalWrite(latch1, HIGH);
- //Turns off player1 and player2 LEDs
- digitalWrite(latch2, LOW);
- shiftOut(data2, clock2, MSBFIRST, 255);
- digitalWrite(latch2, HIGH);
- digitalWrite(latch3, LOW);
- shiftOut(data3, clock3, MSBFIRST, 255);
- digitalWrite(latch3, HIGH);
- pinMode(pot, INPUT);
- strip.begin();
- strip.show();
- pinMode(player1, INPUT);
- digitalWrite(player1, HIGH);
- pinMode(player2, INPUT);
- digitalWrite(player2, HIGH);
- score = selectScore(); //Sets the winning score
- //Turns on player1 LEDs
- digitalWrite(latch2, LOW);
- shiftOut(data2, clock2, MSBFIRST, 0);
- digitalWrite(latch2, HIGH);
- }
- void loop()
- {
- digitalWrite(latch1, LOW);
- shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
- shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
- digitalWrite(latch1, HIGH);
- if(player1Points >= score)
- {
- player1Celebrate();
- }
- if(player2Points >= score)
- {
- player2Celebrate();
- }
- int val = analogRead(pot);
- val = map(val, 0, 1023, 1, 100);
- if(flag == 'F')
- {
- chaseForward(strip.Color(127, 0, 0), val);
- }
- if(flag == 'R')
- {
- chaseReverse(strip.Color(0, 0, 127), val);
- }
- int AbuttonState = digitalRead(player1);
- if(AbuttonState == LOW && turn == 'A' && flag != 'F' && flag != 'R')
- {
- i = 0;
- //Turns off player 1 LEDs
- digitalWrite(latch2, LOW);
- shiftOut(data2, clock2, MSBFIRST, 255);
- digitalWrite(latch2, HIGH);
- chaseForward(strip.Color(0, 127, 0), val); // White
- }
- int BbuttonState = digitalRead(player2);
- if(BbuttonState == LOW && turn == 'B' && flag != 'F' && flag != 'R')
- {
- i = 30;
- //Turns off player 2 LEDs
- digitalWrite(latch3, LOW);
- shiftOut(data3, clock3, MSBFIRST, 255);
- digitalWrite(latch3, HIGH);
- chaseReverse(strip.Color(127, 0, 127), val); // Red
- }
- }
- uint32_t Wheel(uint16_t WheelPos)
- {
- byte r, g, b;
- switch(WheelPos / 128)
- {
- case 0:
- r = 127 - WheelPos % 128; //Red down
- g = WheelPos % 128; // Green up
- b = 0; //blue off
- break;
- case 1:
- g = 127 - WheelPos % 128; //green down
- b = WheelPos % 128; //blue up
- r = 0; //red off
- break;
- case 2:
- b = 127 - WheelPos % 128; //blue down
- r = WheelPos % 128; //red up
- g = 0; //green off
- break;
- }
- return(strip.Color(r,g,b));
- }
- void chaseForward(uint32_t c, uint8_t wait)
- {
- lcd.clear();
- for(i; i < 30; i++)
- {
- if(digitalRead(player2) == LOW)
- cheat = 'Y';
- strip.setPixelColor(i, c); // Set new pixel 'on'
- strip.show(); // Refresh LED states
- strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
- delay(wait);
- if(i > 27 && cheat != 'Y')
- {
- int BbuttonState = digitalRead(player2);
- if(BbuttonState == LOW)
- {
- turn = 'B';
- flag = 'R';
- return;
- }
- }
- }
- player1Points++;
- digitalWrite(latch1, LOW);
- shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
- shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
- digitalWrite(latch1, HIGH);
- if(player1Points < score)
- lcd.print("PLAYER 1 SCORES!");
- strip.show();
- //Turn on player1 LEDs
- digitalWrite(latch2, LOW);
- shiftOut(data2, clock2, MSBFIRST, 0);
- digitalWrite(latch2, HIGH);
- flag = 'S';
- cheat = 'N';
- }
- void chaseReverse(uint32_t c, uint8_t wait)
- {
- lcd.clear();
- for(i; i >= 0; i--)
- {
- if(digitalRead(player1) == LOW)
- cheat = 'Y';
- strip.setPixelColor(i, c); // Set new pixel 'on'
- strip.show(); // Refresh LED states
- strip.setPixelColor(i, 0); // Erase pixel, but don't refresh!
- delay(wait);
- if(i < 2 && cheat != 'Y')
- {
- int BbuttonState = digitalRead(player1);
- if(BbuttonState == LOW)
- {
- turn = 'A';
- flag = 'F';
- return;
- }
- }
- }
- player2Points++;
- digitalWrite(latch1, LOW);
- shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
- shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
- digitalWrite(latch1, HIGH);
- strip.show();
- if(player2Points < score)
- lcd.print("PLAYER 2 SCORES!");
- //Turn on player2 LEDs
- digitalWrite(latch3, LOW);
- shiftOut(data3, clock3, MSBFIRST, 0);
- digitalWrite(latch3, HIGH);
- flag = 'S';
- cheat = 'N';
- }
- void player1Celebrate()
- {
- lcd.print("PLAYER 1 WINS!");
- rainbowCyclePlayer1(0);
- player1Points = 0;
- player2Points = 0;
- digitalWrite(latch1, LOW);
- shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
- shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
- digitalWrite(latch1, HIGH);
- }
- void player2Celebrate()
- {
- lcd.print("PLAYER 2 WINS!");
- rainbowCyclePlayer2(0);
- player1Points = 0;
- player2Points = 0;
- digitalWrite(latch1, LOW);
- shiftOut(data1, clock1, MSBFIRST, ~digitOne[player1Points]);
- shiftOut(data1, clock1, MSBFIRST, ~digitTwo[player2Points]);
- digitalWrite(latch1, HIGH);
- }
- void rainbowCyclePlayer1(uint8_t wait)
- {
- int m = 32;
- uint16_t k, j;
- //Turns off player 1 LEDs
- digitalWrite(latch2, LOW);
- shiftOut(data2, clock2, MSBFIRST, 255);
- digitalWrite(latch2, HIGH);
- for (j=0; j < 384 * 3; j++) { // 5 cycles of all 384 colors in the wheel
- for (k=0; k < strip.numPixels(); k++) {
- // tricky math! we use each pixel as a fraction of the full 384-color wheel
- // (thats the i / strip.numPixels() part)
- // Then add in j which makes the colors go around per pixel
- // the % 384 is to make the wheel cycle around
- strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
- }
- strip.show(); // write all the pixels out
- delay(wait);
- }
- for(m; m >= 0; m--)
- {
- strip.setPixelColor(m, 0);
- strip.show();
- }
- //Turn on player1 LEDs
- digitalWrite(latch2, LOW);
- shiftOut(data2, clock2, MSBFIRST, 0);
- digitalWrite(latch2, HIGH);
- }
- void rainbowCyclePlayer2(uint8_t wait)
- {
- int m = 0;
- uint16_t k, j;
- //Turns off player 2 LEDs
- digitalWrite(latch3, LOW);
- shiftOut(data3, clock3, MSBFIRST, 255);
- digitalWrite(latch3, HIGH);
- for (j=384 * 3; j > 0; j--) { // 5 cycles of all 384 colors in the wheel
- for (k=0; k < strip.numPixels(); k++) {
- // tricky math! we use each pixel as a fraction of the full 384-color wheel
- // (thats the i / strip.numPixels() part)
- // Then add in j which makes the colors go around per pixel
- // the % 384 is to make the wheel cycle around
- strip.setPixelColor(k, Wheel( ((k * 384 / strip.numPixels()) + j) % 384) );
- }
- strip.show(); // write all the pixels out
- delay(wait);
- }
- for(m=0; m < 32; m++)
- {
- strip.setPixelColor(m, 0);
- strip.show();
- }
- //Turn on player2 LEDs
- digitalWrite(latch3, LOW);
- shiftOut(data3, clock3, MSBFIRST, 0);
- digitalWrite(latch3, HIGH);
- }
- int selectScore()
- {
- lcd.print("Player 1, select");
- lcd.setCursor(0, 1);
- lcd.print("score limit by");
- delay(3000);
- lcd.setCursor(1,0);
- lcd.clear();
- lcd.print("pressing button.");
- delay(3000);
- lcd.setCursor(1,0);
- lcd.clear();
- lcd.print("Player 2");
- lcd.setCursor(0,1);
- lcd.print("confirms by");
- delay(3000);
- lcd.setCursor(1,0);
- lcd.clear();
- lcd.print("pressing their");
- lcd.setCursor(0,1);
- lcd.print("button.");
- delay(3000);
- lcd.clear();
- int i;
- int buttonState;
- int lastButtonState;
- int buttonPushCounter=1;
- int confirm=1;
- while(confirm==1) //Stay in this loop
- {
- delay(100); //The number will look really dim if you take this out
- lcd.clear();
- lcd.print(buttonPushCounter);
- buttonState = digitalRead(player1);
- confirm = digitalRead(player2);
- if (buttonState != lastButtonState)
- {
- if (buttonState == LOW)
- {
- buttonPushCounter++;
- }
- }
- lastButtonState = buttonState;
- if (buttonPushCounter > 9) //start over at 1
- {
- lcd.clear();
- lcd.print(1);
- buttonPushCounter = 1;
- }
- if (confirm == LOW)
- {
- lcd.clear();
- return buttonPushCounter;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement