Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <LiquidCrystal.h>
- LiquidCrystal lcd(8, 9, 4, 5, 6, 7); // select the pins used on the LCD panel
- // define some values used by the panel and buttons
- int lcd_key = 0;
- int adc_key_in = 0;
- #define btnRIGHT 0
- #define btnUP 1
- #define btnDOWN 2
- #define btnLEFT 3
- #define btnSELECT 4
- #define btnNONE 5
- int read_LCD_buttons(){ // read the buttons
- adc_key_in = analogRead(0); // read the value from the sensor
- // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
- // we add approx 50 to those values and check to see if we are close
- // We make this the 1st option for speed reasons since it will be the most likely result
- if (adc_key_in > 1000) return btnNONE;
- // For V1.0 comment the other threshold and use the one below:
- if (adc_key_in < 50) return btnRIGHT;
- if (adc_key_in < 195) return btnUP;
- if (adc_key_in < 380) return btnDOWN;
- if (adc_key_in < 555) return btnLEFT;
- if (adc_key_in < 790) return btnSELECT;
- return btnNONE; // when all others fail, return this.
- }
- const boolean inputMode = HIGH;// high for buttons LOW for piezo
- const int numPorts=2;
- int led[numPorts]={31,33};
- int ins[numPorts]={A8,A9};
- int goButton=btnSELECT;
- int rotarySwitch[3]={22,24,26};
- int theDelay=1;
- unsigned long time1;
- unsigned long time2;
- float interval1;
- void setup()
- {
- pinMode(goButton,INPUT);
- digitalWrite(goButton,HIGH);
- for (int i=0; i<numPorts; i++)
- {
- pinMode(led[i], OUTPUT);
- pinMode(ins[i],INPUT);
- //if (inputMode==HIGH) digitalWrite(ins[i],HIGH); // activate internal pullup;
- }
- for (int i=0; i<3; i++)
- {
- pinMode(rotarySwitch[i],INPUT);
- digitalWrite(rotarySwitch[i],HIGH);//internal pullup
- }
- Serial.begin(9600);
- lcd.print(0x0C); delay(5);
- doPost();
- lcd.print(0x0C); delay(5);
- lcd.print(0xFA); //define custom character 2
- //Now send the eight data bytes
- lcd.print(B00000100);
- lcd.print(B00000100);
- lcd.print(B00011111);
- lcd.print(B00000100);
- lcd.print(B00000100);
- lcd.print(B00000100);
- lcd.print(B00000000);
- lcd.print(B00011111);
- //Serial1.write(0x02); //Display the new custom character 2
- lcd.print(0x11);
- lcd.print(0x0C); delay(5);
- // 0000000000111111
- // 0123456789012345
- lcd.println("Select a mode ");
- lcd.println("then press GO ");
- }
- void loop()
- {
- //Serial1.write(0x12);
- //delay(1000);
- lcd.print(0x0C); delay(5);
- // 0000000000111111
- // 0123456789012345
- lcd.println("Select a mode ");
- lcd.println("press RUN ");
- while ((PINC & B00010000));
- //doRapidFire();
- if (digitalRead(rotarySwitch[0])==LOW) doQuickDraw();
- else if (digitalRead(rotarySwitch[1])==LOW) doTimedMode();
- else if (digitalRead(rotarySwitch[2])==LOW) doRapidFire();
- //doQuickDraw();
- //doTimedMode();
- //doRapidFire();
- }
- void doTimedMode()
- {
- randomSeed(millis());
- int currentPort=random(4);
- int newPort=random(4);
- int maxRounds=32;
- int hitCounter=0;
- time1=millis();
- interval1=0;
- lcd.print(0x0C); delay(5);
- // 0000000000111111
- lcd.println("Timed mode");
- delay(3000);
- lcd.print(0x0C); delay(5);
- lcd.print(0xDF); // C note
- lcd.println("GO");
- while (interval1 < 10000) // 10 seconds
- {
- //time1=millis();
- digitalWrite(led[currentPort],HIGH);
- switch (currentPort)
- {
- case 0:
- while (!(PINC & B00000001));
- break;
- case 1:
- while (!(PINC & B00000010));
- break;
- case 2:
- while (!(PINC & B00000100));
- break;
- case 3:
- while (!(PINC & B00001000));
- break;
- }
- hitCounter++;
- //delay(1000);
- time2=millis();
- interval1=(time2-time1);
- digitalWrite(led[currentPort],LOW);
- lcd.print(0x0C); delay(5);
- // 0000000000111111
- lcd.print("Hits: ");
- lcd.println(hitCounter,DEC);
- newPort=random(4);
- while (newPort==currentPort) newPort=random(4);
- currentPort=newPort;
- }
- lcd.print(0x0C); delay(5);
- lcd.print(hitCounter,DEC);
- lcd.println(" Hits");
- lcd.print(0x0D);
- lcd.println("in 10Secs");
- delay(5000);
- }
- void doRapidFire()
- {
- randomSeed(millis());
- int currentPort=random(4);
- int newPort=random(4);
- int maxRounds=32;
- time1=millis();
- lcd.print(0x0C); delay(5);
- // 0000000000111111
- lcd.println("Rapid Fire mode");
- delay(2000);
- lcd.print(0xDF); // C note
- for (int i=0; i<maxRounds; i++)
- {
- lcd.print(0x0C); delay(10);
- // 0000000000111111
- lcd.println("Rapid Fire mode");
- lcd.print("Round ");
- lcd.print(i+1,DEC);
- lcd.print(" of ");
- lcd.print(maxRounds,DEC);
- lcd.println("");
- digitalWrite(led[currentPort],HIGH);
- switch (currentPort)
- {
- case 0:
- while (!(PINC & B00000001));
- break;
- case 1:
- while (!(PINC & B00000010));
- break;
- case 2:
- while (!(PINC & B00000100));
- break;
- case 3:
- while (!(PINC & B00001000));
- break;
- }
- //delay(1000);
- //interval1=interval1+0.00001;
- digitalWrite(led[currentPort],LOW);
- newPort=random(4);
- while (newPort==currentPort) newPort=random(4);
- //newPort=3;
- currentPort=newPort;
- }
- time2=millis();
- interval1=(time2-time1);
- interval1=interval1/1000;
- lcd.print(0x0C); delay(5);
- lcd.println("Rapid Time:"); lcd.print(0x0D);
- lcd.print(interval1,4);
- lcd.println(" secs");
- delay(5000);
- }
- void doQuickDraw()
- {
- randomSeed(millis());
- int firstTime=true;
- int currentPort=random(4);
- int maxRounds=8;
- lcd.print(0x0C); delay(5);
- lcd.println("Quickdraw mode ");
- delay(2000);
- for (int i=0; i<maxRounds; i++)
- {
- lcd.print(0x0C); delay(5);
- lcd.println("Quickdraw mode ");
- lcd.print("Round ");
- lcd.print(i+1,DEC);
- lcd.print(" of ");
- lcd.print(maxRounds,DEC);
- lcd.println("");
- delay(random(3000)+1000);
- if (firstTime==true) { lcd.print(0xDF); firstTime=false; }// C note
- time1=millis();
- digitalWrite(led[currentPort],HIGH);
- switch (currentPort)
- {
- case 0:
- while (!(PINC & B00000001));
- break;
- case 1:
- while (!(PINC & B00000010));
- break;
- case 2:
- while (!(PINC & B00000100));
- break;
- case 3:
- while (!(PINC & B00001000));
- break;
- }
- //delay(1000);
- time2=millis();
- interval1=(time2-time1);
- interval1=interval1/1000;
- digitalWrite(led[currentPort],LOW);
- lcd.print(0x0C); delay(5);
- lcd.println("Quickdraw Time: ");
- lcd.print(interval1,3);
- lcd.println(" seconds");
- delay(2000);
- currentPort=random(4);
- }
- delay(5000);
- }
- void doPost()
- {
- lcd.print(0xD2); // 1/16 note
- lcd.print(0xE3);//E
- //delay(100);
- lcd.print(0xDC);//A
- //delay(100);
- lcd.print(0xDE);//B
- for (int lop=0; lop<20; lop++)
- {
- theDelay+=10;
- if (theDelay>500) theDelay=1;
- for (int i=0; i<numPorts; i++)
- {
- //if (digitalRead(ins[i])==HIGH)
- {
- digitalWrite(led[i], HIGH); // turn the LED on (HIGH is the voltage level)
- }
- }
- delay(theDelay); // wait for a second
- for (int i=0; i<numPorts; i++)
- {
- digitalWrite(led[i], LOW); // turn the LED off by making the voltage LOW
- }
- delay(theDelay); // wait for a second
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment