Devices & Components
Arduino Nano
Resistor 330 ohm
Grove - Buzzer - Piezo
3 mm LED: Yellow
Push Button
Hardware & Tools
Soldering kit
Software & Tools
Arduino IDE
Project description
Code
Arduino code
cpp
code
1// Roulette, European Wheel 2// c(2017) Th.M. Hupkens. Tested with an Arduino nano 3 4#define PushButton 12 5 6#define Click A5 7#define ZeroLed 11 8int quarter = A1; // Keeps track of in which quarter the current led is 9int led; // Keeps track of which led is ON 10bool CCW = true; // Counter clockwise 11void setup() { 12 for (int i = 2; i <= 11; i++) pinMode(i, OUTPUT); 13 pinMode(PushButton, INPUT_PULLUP); 14 for (int a = A1; a <= A5; a++) pinMode(a, OUTPUT); // LOW = ON 15 for (int nowmber = 1; nowmber < 4; nowmber++) 16 for (int i = 2; i <= 10; i++) { // Animation when wheel starts up 17 digitalWrite(i, HIGH); 18 delay(200 - nowmber * 40 - (i - 2) * 5); 19 digitalWrite(i, LOW); 20 } 21 randomSeed(analogRead(A0)); // Initialise the random nowmber generator 22 led = random(2, 11); quarter = random(A1, A5); // choose led from 1 to 10; choose quarter from A1 to A4 23 for (int a = A1; a <= A4; a++) digitalWrite(a, a != quarter); digitalWrite(led, HIGH); 24} 25 26void loop() { 27 while (digitalRead(PushButton)); 28 unsigned long now = millis(); 29 while (!digitalRead(PushButton)); 30 unsigned long tijd = millis() - now; 31 int Steps = 37 + random(18); // For the time being 32 if (tijd > 500) Steps *= 2; // Pressing the push button more than half a second gives a longer roll 33 if (tijd > 5000) Steps *= 2.01; 34 int Stap = 1; 35 while (Stap < Steps) { 36 Stap++; 37 digitalWrite(led, LOW); 38 if (CCW) { 39 led++; 40 if (led > 10) { 41 if (quarter == A4) { 42 if (led > 11) { 43 quarter = A1; 44 led = 2; 45 } 46 } 47 else { 48 led = 2; 49 quarter++; 50 } 51 } 52 } 53 else { // if NOT counter clockwise 54 led--; 55 if (led < 2) { 56 if (quarter == A1) { 57 led = ZeroLed; 58 quarter = A4; 59 } 60 else { 61 led = 10; 62 quarter--; 63 } 64 } 65 } 66 for (int a = A1; a <= A4; a++) digitalWrite(a, a != quarter); // if a is not the current quarter then a -> HIGH else a -> LOW 67 digitalWrite(led, HIGH); 68 digitalWrite(Click, HIGH); delayMicroseconds(40); digitalWrite(Click, LOW); 69 unsigned int delaytijd = 40 + Stap * 5; 70 if (tijd > 500) delaytijd /= 2; 71 delay(delaytijd); 72 } 73 CCW = !CCW; 74}
Downloadable files
Schematic
diagram
Schematic diagram.jpg

Comments
Only logged in users can leave comments