Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<LiquidCrystal.h>
- LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
- const int ButtonPin1 = 9;
- #define inputCLK 6
- #define inputDT 7
- int piezo = 8;
- int notes[] = {261, 294, 329, 349, 392, 440, 493, 523};
- //order: c d e f g a b C
- int counter = 0;
- int currentStateCLK;
- int previousStateCLK;
- int B1State = 0;
- bool piezoMode;
- void setup() {
- pinMode (inputCLK,INPUT);
- pinMode (inputDT,INPUT);
- pinMode(ButtonPin1, INPUT);
- Serial.begin (9600);
- lcd.begin(16,2);
- lcd.print("position:" );
- }
- void loop() {
- B1State = digitalRead(ButtonPin1);
- if (B1State == HIGH) {
- randomNumber();
- }
- else {
- previousStateCLK = digitalRead(inputCLK);
- currentStateCLK = digitalRead(inputCLK); // Reads the "current" state of the inputCLK
- if (currentStateCLK != previousStateCLK){
- if (digitalRead(inputDT) != currentStateCLK) {
- counter ++;
- } else {
- counter --;
- }
- if (counter<0){
- counter=0;
- }
- else if (counter>9){
- counter=9;
- }
- if (counter == 1){
- tone (piezo, notes[0]);
- }
- else if (counter == 2){
- tone (piezo, notes[1]);
- }
- else if (counter == 3){
- tone (piezo, notes[2]);
- }
- else if (counter == 4){
- tone (piezo, notes[3]);
- }
- else if (counter == 5){
- tone (piezo, notes[4]);
- }
- else if (counter == 6){
- tone (piezo, notes[5]);
- }
- else if (counter == 7){
- tone (piezo, notes[6]);
- }
- else if (counter == 8){
- tone (piezo, notes[7]);
- }
- else {
- noTone(8);
- }
- Serial.print("Position: ");
- Serial.println(counter);
- previousStateCLK = currentStateCLK; // Updates the previous state of the inputCLK with the current state
- }
- }
- }
- void randomNumber() {
- int i = random(0, 7);
- tone(piezo, notes[i]);
- delay (1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement