Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const int buttonPin = 2; // the number of the pushbutton pin
- const int buttonPin2 = 3; // the number of the pushbutton pin
- // variables will change:
- int buttonState = LOW; // variable for reading the pushbutton status
- int buttonState2 = LOW; // variable for reading the pushbutton status
- int ledSpeed = 100;
- int piezoBuz = 7;
- int leftPlayerLed = 4;
- int rightPlayerLed = 5;
- #define NUMLIGHTS 16
- int pins[NUMLIGHTS] = { 24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 };
- int leftPlayer2rightPlayer;
- #define leftP2rightP 0
- #define rightP2leftP 1
- int lightPin;
- void(* resetFunc) (void) = 0;//declare reset function at address 0
- void setup() {
- // put your setup code here, to run once:
- pinMode(buttonPin, INPUT);
- pinMode(buttonPin2, INPUT);
- // pinMode(pins[NUMLIGHTS], OUTPUT);
- pinMode(piezoBuz, OUTPUT);
- for (lightPin=0 ; lightPin < NUMLIGHTS ; lightPin++) {
- pinMode(pins[lightPin], OUTPUT);
- }
- delay(3000);
- }
- void loop() {
- static int pos = 0; // the position of the brightest light in the light array
- static int direction = 1; // the direction the bright spot is travelling (1 or -1)
- int light;
- int inten = 200;
- if (inten > 255) inten = 255;
- for (light=0 ; light < NUMLIGHTS ; light++) {
- if (light == pos) { // The light at this position is set bright
- analogWrite(pins[light], inten);
- } else if ( light == (pos+1) || light == (pos-1)) {
- // This makes the two lights adjacent to the bright one glow at reduced intensity.
- // It makes for a nicer effect
- analogWrite(pins[light], inten>>4);
- } else {
- // Digital I/O pins 5 & 6 don't seem to go dark if I do analogWrite(pins[light], 0)
- // By doing digitalWrite it all looks correct
- digitalWrite(pins[light], 0);
- }
- }
- if (ledSpeed <10)
- {ledSpeed = 5;}
- pos += direction;
- // if we've reached the end, reverse directions
- buttonState2 = digitalRead(buttonPin2);
- // check if the pushbutton is pressed.
- // if it is, the buttonState is HIGH:
- if (buttonState2 == HIGH && pos == 16 || buttonState2 == HIGH && pos == 15) {
- direction = -1;
- ledSpeed = ledSpeed -4;
- }
- // read the state of the pushbutton value:
- buttonState = digitalRead(buttonPin);
- // check if the pushbutton is pressed.
- // if it is, the buttonState is HIGH:
- if (buttonState == HIGH && pos == 0 || buttonState == HIGH && pos == 1) {
- direction = 1;
- ledSpeed = ledSpeed -4;
- }
- if (pos > 16)
- {
- for (int x = 0; x<5; x++) {
- analogWrite(rightPlayerLed, 255);
- delay(100);
- analogWrite(rightPlayerLed, 0);
- delay(100);
- }
- tone(piezoBuz, 250, 100);
- if (buttonState == HIGH && buttonState2 == HIGH) {
- resetFunc(); //call reset
- }
- } else if (pos <0 )
- {
- for (int x = 0; x<5; x++) {
- analogWrite(leftPlayerLed, 255);
- delay(100);
- analogWrite(leftPlayerLed, 0);
- delay(100);
- }
- tone(piezoBuz, 250, 100);
- if (buttonState == HIGH && buttonState2 == HIGH) {
- resetFunc(); //call reset
- }
- }
- delay(ledSpeed);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement