Advertisement
Hanneman

Spelletje

Jul 27th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. const int buttonPin = 2;     // the number of the pushbutton pin
  2. const int buttonPin2 = 3;     // the number of the pushbutton pin
  3.  
  4. // variables will change:
  5. int buttonState = LOW;         // variable for reading the pushbutton status
  6. int buttonState2 = LOW;         // variable for reading the pushbutton status
  7.  
  8. int ledSpeed = 100;
  9.  
  10. int piezoBuz = 7;
  11.  
  12. int leftPlayerLed = 4;
  13. int rightPlayerLed = 5;
  14.  
  15. #define NUMLIGHTS 16
  16. int pins[NUMLIGHTS] = { 24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39 };
  17.  
  18. int leftPlayer2rightPlayer;
  19.  
  20. #define leftP2rightP 0
  21. #define rightP2leftP 1
  22.  
  23. int lightPin;
  24.  
  25. void(* resetFunc) (void) = 0;//declare reset function at address 0
  26.  
  27. void setup() {
  28. // put your setup code here, to run once:
  29. pinMode(buttonPin, INPUT);    
  30. pinMode(buttonPin2, INPUT);
  31. //  pinMode(pins[NUMLIGHTS], OUTPUT);
  32. pinMode(piezoBuz, OUTPUT);
  33.  
  34. for (lightPin=0 ; lightPin < NUMLIGHTS ; lightPin++) {
  35.   pinMode(pins[lightPin], OUTPUT);
  36. }
  37.     delay(3000);
  38. }
  39.  
  40. void loop() {
  41.  
  42.   static int pos = 0;       // the position of the brightest light in the light array
  43.   static int direction = 1; // the direction the bright spot is travelling (1 or -1)
  44.  
  45.   int light;
  46.  
  47.   int inten = 200;
  48.  
  49.   if (inten > 255) inten = 255;
  50.  
  51.   for (light=0 ; light < NUMLIGHTS ; light++) {
  52.     if (light == pos) {  // The light at this position is set bright      
  53.       analogWrite(pins[light], inten);
  54.     } else if ( light == (pos+1) || light == (pos-1)) {
  55.       // This makes the two lights adjacent to the bright one glow at reduced intensity.
  56.       // It makes for a nicer effect
  57.       analogWrite(pins[light], inten>>4);
  58.     } else {
  59.       // Digital I/O pins 5 & 6 don't seem to go dark if I do analogWrite(pins[light], 0)
  60.       // By doing digitalWrite it all looks correct
  61.       digitalWrite(pins[light], 0);
  62.     }    
  63.   }
  64.  
  65.     if (ledSpeed <10)
  66.     {ledSpeed = 5;}
  67.    
  68.   pos += direction;
  69.   // if we've reached the end, reverse directions
  70.   buttonState2 = digitalRead(buttonPin2);
  71.   // check if the pushbutton is pressed.
  72.   // if it is, the buttonState is HIGH:
  73.   if (buttonState2 == HIGH && pos == 16 || buttonState2 == HIGH && pos == 15) {
  74.     direction = -1;
  75.     ledSpeed = ledSpeed -4;
  76.   }
  77.   // read the state of the pushbutton value:
  78.   buttonState = digitalRead(buttonPin);
  79.   // check if the pushbutton is pressed.
  80.   // if it is, the buttonState is HIGH:
  81.   if (buttonState == HIGH && pos == 0 || buttonState == HIGH && pos == 1) {
  82.     direction = 1;
  83.     ledSpeed = ledSpeed -4;
  84.   }
  85.   if (pos > 16)
  86.   {
  87.  
  88.     for (int x = 0; x<5; x++) {
  89.       analogWrite(rightPlayerLed, 255);
  90.       delay(100);
  91.        analogWrite(rightPlayerLed, 0);
  92.        delay(100);
  93.       }
  94.  
  95.       tone(piezoBuz, 250, 100);
  96.      
  97.            if (buttonState == HIGH && buttonState2 == HIGH) {
  98.               resetFunc(); //call reset  
  99.     }  
  100.    
  101.   } else if (pos <0 )
  102.   {
  103.     for (int x = 0; x<5; x++) {
  104.       analogWrite(leftPlayerLed, 255);
  105.       delay(100);
  106.        analogWrite(leftPlayerLed, 0);
  107.        delay(100);
  108.       }
  109.      
  110.        tone(piezoBuz, 250, 100);
  111.              if (buttonState == HIGH && buttonState2 == HIGH) {
  112.                  resetFunc(); //call reset
  113.   }
  114.  
  115.   }
  116.   delay(ledSpeed);
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement