Advertisement
Guest User

Code

a guest
May 22nd, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.62 KB | None | 0 0
  1. const int ledPin[2] = {5,6};
  2. const int button1pin = 2;
  3. const int button2pin = 3;
  4. const int numChoices = 4;
  5. int choices[numChoices] = {0, 1, 1, 0};
  6. int currentChoice = 0;
  7. bool isRecording = false;
  8. bool 1hasBeenPressed = false;
  9. bool 2hasBeenPressed = false;
  10.  
  11. int buttonDebounce(int buttonPin) {
  12.   int oldState;
  13.   int currentState;
  14.   oldState = digitalRead(buttonPin);
  15.   delay(50);
  16.   if (oldState == digitalRead(buttonPin));
  17.   {
  18.     currentState = digitalRead(buttonPin);
  19.   }
  20.   return currentState;
  21. }
  22.  
  23. void record() {
  24.   Serial.println("record");
  25.   currentChoice = 0;
  26.   while(currentChoice < numChoices)
  27.   {
  28.       if(buttonDebounce(button2pin) == HIGH && 1hasBeenPressed == false)
  29.        {
  30.         1hasBeenPressed = true;
  31.         addChoice(1);
  32.        }
  33.       if(buttonDebounce(button1pin) == HIGH && 2hasBeenPressed == false)
  34.         {
  35.          2hasBeenPressed = true;
  36.          addChoice(0);
  37.         }
  38.       if(digitalRead(button1pin == LOW)){
  39.           if(buttonDebounce(button1pin) == LOW)
  40.            {
  41.             1hasBeenPressed = false;
  42.            }
  43.        }
  44.       if(digitalRead(button2pin == LOW)){
  45.           if(buttonDebounce(button1pin) == LOW)
  46.            {
  47.             2hasBeenPressed = false;
  48.            }
  49.        }
  50.  
  51.   }
  52.   if (currentChoice >= numChoices)
  53.   {
  54.     currentChoice = 0;
  55.   }
  56. }
  57.  
  58. void addChoice(int buttonNum) {
  59.   choices[currentChoice] = buttonNum;
  60.   currentChoice += 1;
  61.   Serial.println(currentChoice);
  62.   Serial.println(buttonNum);
  63. }
  64.  
  65. void play() {
  66.   Serial.println("play");
  67.   currentChoice = 0;
  68.   while(currentChoice < 4)
  69.   {
  70.     Serial.print("led");
  71.     analogWrite(ledPin[choices[currentChoice]], 50);
  72.     delay(1000);
  73.     analogWrite(ledPin[choices[currentChoice]], 0);
  74.     delay(500);
  75.     currentChoice += 1;
  76.   }
  77.   if (currentChoice >= 4)
  78.   {
  79.     currentChoice = 0;
  80.   }
  81. }
  82.  
  83. void setup() {
  84.   // put your setup code here, to run once:
  85.   Serial.begin(9600);
  86.   pinMode(button1pin, INPUT);
  87.   pinMode(button2pin, INPUT);
  88. }
  89.  
  90. void loop() {
  91.   // put your main code here, to run repeatedly:
  92.   //Serial.println("enter loop");
  93.    if(digitalRead(button1pin == LOW)){
  94.           if(buttonDebounce(button1pin) == LOW)
  95.            {
  96.             1hasBeenPressed = false;
  97.            }
  98.        }
  99.    if(digitalRead(button2pin == LOW)){
  100.           if(buttonDebounce(button1pin) == LOW)
  101.            {
  102.             2hasBeenPressed = false;
  103.            }
  104.        }
  105.   if(digitalRead(button1pin) == HIGH && 1hasBeenPressed == false)
  106.   {
  107.     record();
  108.   }
  109.   if(digitalRead(button2pin) == HIGH && 2hasBeenPressed == false)
  110.   {
  111.     play();
  112.   }
  113.     //Serial.println("end loop");
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement