commit 98bffa26630c68a099111ce3a4b4267945e2f6c7 Author: Lorenzo DellacĂ  Date: Mon Dec 7 18:24:57 2020 +0100 Initial commit including DiceRoller and VolumeMixer diff --git a/DiceRoller/DiceRoller.ino b/DiceRoller/DiceRoller.ino new file mode 100644 index 0000000..be86890 --- /dev/null +++ b/DiceRoller/DiceRoller.ino @@ -0,0 +1,86 @@ + + +int isGenerating = false; + +//Connesso al Pin DS del 74HC595 +int dataPin = 8; +//Connesso al Pin ST_CP del 74HC595 +int latchPin = 9; +//Connesso al Pin SH_CP del 74HC595 +int clockPin = 10; + +int buttonPin = 12; + + +void setup() { + //configuriamo i 3 pin di output + pinMode(dataPin, OUTPUT); + pinMode(latchPin, OUTPUT); + pinMode(clockPin, OUTPUT); + + pinMode(buttonPin, INPUT); + + turnOnLeds(0); + +} + + +void loop() { + + + if (!isGenerating) + { + + if (digitalRead(buttonPin) == HIGH) + { + isGenerating = true; + int wait = 10; + + for (int i = 0; i <= 20; i++) + { + int rand = random(1, 7); + + turnOnLeds(rand); + + wait += 10; + + delay(wait); + } + + isGenerating = false; + } + } +} + +void turnOnLeds(int howMany) +{ + for (int j = 0; j < 7 - howMany; j++) + { + + digitalWrite(clockPin, 0); //mi assicuro che clock si LOW + digitalWrite(latchPin, 0); //abbasso il latch per iniziare a programmare il 74HC595 + + digitalWrite(dataPin, 0); // abbasso il pin data + digitalWrite(clockPin, 1); //alzo il clock -> abbiamo "spinto" il valore 1 nel primo registro del 74HC595. + + digitalWrite(dataPin, 0); //rimetto a zero il data pin e clock pin per non portare valori HIGH oltre questo punto. + digitalWrite(clockPin, 0); //rimetto a zero il clock pin. + + digitalWrite(latchPin, 1); //latch HIGH per confermare gli output. + } + + for (int j = 0; j < howMany; j++) { + digitalWrite(clockPin, 0); //mi assicuro che clock si LOW + digitalWrite(latchPin, 0); //abbasso il latch per iniziare a programmare il 74HC595 + + digitalWrite(dataPin, 1); // alzo il pin data + digitalWrite(clockPin, 1); //alzo il clock -> abbiamo "spinto" il valore 1 nel primo registro del 74HC595. + + digitalWrite(dataPin, 0); //rimetto a zero il data pin e clock pin per non portare valori HIGH oltre questo punto. + digitalWrite(clockPin, 0); //rimetto a zero il clock pin. + + digitalWrite(latchPin, 1); //latch HIGH per confermare gli output. + } + + +} \ No newline at end of file diff --git a/VolumeMixer/VolumeMixer.ino b/VolumeMixer/VolumeMixer.ino new file mode 100644 index 0000000..9f24fc9 --- /dev/null +++ b/VolumeMixer/VolumeMixer.ino @@ -0,0 +1,99 @@ + +int vol0, vol1, vol2, vol3, vol4; + +int changeRange = 20; + +int red = 9; +int green = 10; +int blue = 11; + + +// The setup() function runs once each time the micro-controller starts +void setup() +{ + + pinMode(A0, INPUT); + pinMode(A1, INPUT); + pinMode(A2, INPUT); + pinMode(A3, INPUT); + pinMode(A4, INPUT); + + + pinMode(9, OUTPUT); + pinMode(10, OUTPUT); + pinMode(11, OUTPUT); + + + Serial.begin(4800); + + while (!Serial); + Serial.println(); + Serial.println("OK"); + Serial.flush(); + + + vol0 = analogRead(A0); + vol1 = analogRead(A1); + vol2 = analogRead(A2); + vol3 = analogRead(A3); + vol4 = analogRead(A4); + + analogWrite(red, 180); + analogWrite(green, 0); + analogWrite(blue, 200); + +} + +// Add the main program code into the continuous loop() function +void loop() +{ + delay(400); + + int pot0 = analogRead(A0); + int pot1 = analogRead(A1); + int pot2 = analogRead(A2); + int pot3 = analogRead(A3); + int pot4 = analogRead(A4); + + if (shouldChangeVolume(vol0, pot0)) + { + Serial.print("NUOVO VALORE 0: "); + Serial.println(pot0); + vol0 = pot0; + } + + if (shouldChangeVolume(vol1, pot1)) + { + Serial.print("NUOVO VALORE 1: "); + Serial.println(pot1); + vol1 = pot1; + } + if (shouldChangeVolume(vol2, pot2)) + { + Serial.print("NUOVO VALORE 2: "); + Serial.println(pot2); + vol2 = pot2; + } + if (shouldChangeVolume(vol3, pot3)) + { + Serial.print("NUOVO VALORE 3: "); + Serial.println(pot3); + vol3 = pot3; + } + if (shouldChangeVolume(vol4, pot4)) + { + Serial.print("NUOVO VALORE 4: "); + Serial.println(pot4); + vol4 = pot4; + } + +} + +boolean shouldChangeVolume(int oldValue, int newValue) +{ + + int diff = oldValue - newValue; + + if (diff < -changeRange || diff > changeRange) return true; + return false; +}