-
1Circit
It is only wired speaker and button.
The pin no of wired are written on program.
button = obniz.wired("Button", {signal:6 , gnd:7 }); speaker = obniz.wired("Speaker", {signal:0 , gnd: 1});
-
2Rotate image
On HTML, you can use "css transform". For example , this is the code of rotate image 90 degree.
document.getElementById("roulette").style = "transform:rotate(90deg);";
To start and stop rotate slowly, add a var
speed
for rotate degree per frame.let speed = 0; let deg = 0; function rotate(){ deg += speed; document.getElementById("roulette").style = "transform:rotate("+deg+"deg);"; } setInterval(rotate,10);
-
3Beep
Do you want to beep on the roulette no change? With this, you can beep on 440Hz 10ms.
speaker.play(440); await obniz.wait(10); speaker.stop();
This is how to know on change of roulette no.
if( Math.floor((deg + speed) / (360/7.0)) - Math.floor(deg / (360/7.0)) >= 1){ onRouletteChange(); }
So, this is the code of rotate and beep.
let speed = 0; let deg = 0; function rotate(){ //on change value if( Math.floor((deg + speed) / (360/7.0)) - Math.floor(deg / (360/7.0)) >= 1){ onRouletteChange(); } deg += speed; document.getElementById("roulette").style = "transform:rotate("+deg+"deg);"; } setInterval(rotate,10); async function onRouletteChange(){ if(!speaker){return;} speaker.play(440); await obniz.wait(10); speaker.stop(); }
-
4Start when button is pressed
To know button state, add var
buttonState
and set value of current button state.button.onchange = function(pressed){ buttonState = pressed; };
And also add var
phase
for current state of roulette.phase
is setted one of this.const PHASE_WAIT_FOR_START = 0; const PHASE_ROTATE = 1; const PHASE_STOPPING = 2; const PHASE_STOPPED = 3;
For example, when
phase
isPHASE_WAIT_FOR_START
and you want to next phase.if(phase == PHASE_WAIT_FOR_START){ speed = 0; if(buttonState){ phase = PHASE_ROTATE; } }
To speed up rulette, change var
speed
.if(phase == PHASE_ROTATE){ speed = speed+0.5; }
To speed down rulette, change var
speed
.if(phase == PHASE_STOPPING){ speed = speed-0.2; }
Those are component of roulette. Let's make it!
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.