obniz developer team
Published © GPL3+

Colorful Roulette Game with obniz

Create and play a casino-like roulette game with obniz.

BeginnerFull instructions provided4 hours876

Things used in this project

Hardware components

obniz
Cambrian Robotics obniz
IoT development board
×1
Bread board
obniz DIY Electronics Kit
×1
Button
obniz DIY Electronics Kit
×1
Piezo Speaker
obniz DIY Electronics Kit
×1
iPhone
Apple iPhone
smartphone
×1
Android device
Android device
×1

Story

Read more

Schematics

Circuit

Code

Program

JavaScript
<!-- HTML Example -->

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <script src="https://unpkg.com/obniz@1.6.1/obniz.js" crossorigin="anonymous"></script>
  <style>
 
#roulette {
  position : absolute ;
   padding:0px 0 0;
   width:300px;
}

#roulette  img {
   width:300px;
}
#wrapper{
   			position:absolute;
}
#hari {
  position : absolute ;
   top:-20px;
   left:134px;
  z-index : 10;
}
  </style>
</head>
<body>

<div id="obniz-debug"></div>
<h1>obniz roulette</h1>

  <div id="wrapper">
<div id="hari"><img src="https://web.obniz.io/wp-content/uploads/2018/07/pin-1.png"></div>

<div id="roulette"><img src="https://web.obniz.io/wp-content/uploads/2018/07/roulette@3x.png"/></div>

  </div>


<script>
var obniz = new Obniz("OBNIZ_ID_HERE");
let button,speaker;
let buttonState = false;
obniz.onconnect = async function () {

  button = obniz.wired("Button", {signal:6 , gnd:7 });
  speaker = obniz.wired("Speaker", {signal:0 , gnd: 1});
  
  button.onchange = function(pressed){
    buttonState = pressed;
  };
  
  obniz.display.clear();
  obniz.display.print("Start Roulette");
  
}

const PHASE_WAIT_FOR_START = 0; 
const PHASE_ROTATE = 1;
const PHASE_STOPPING = 2;
const PHASE_STOPPED = 3;
let phase = PHASE_WAIT_FOR_START; 
let stopCount = 0;
  
obniz.repeat(async function(){
  if(!button){return;}
  
  if(phase == PHASE_WAIT_FOR_START){
    speed = 0;
  	if(buttonState){
       phase = PHASE_ROTATE;
      
       // wait for release button
      await new Promise((resolve)=>{
        setInterval(()=>{
        	if(!buttonState){
      			resolve();
      		}
        },10);
      });
    }
  }else if(phase == PHASE_ROTATE){
    speed = Math.min(speed+0.5, 5);
    
    if(buttonState){
      phase = PHASE_STOPPING;
      stopCount = 0;
    }
    
  }else if(phase == PHASE_STOPPING){
    
    speed = Math.max(speed-0.2,0);
    if(speed == 0 ){
    	phase = PHASE_STOPPED;
    }
    
  }else if(phase == PHASE_STOPPED){
    for(let i = 0; i < 15;i++){
    	speaker.play(440*3);
  		await obniz.wait(10);
  		speaker.stop(); 
  		await obniz.wait(100);
    }
    phase = PHASE_WAIT_FOR_START;
  }       
             
}, 100);
  
  
  let speed = 0;
  let deg = 0;
  function rotate(){
    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(); 
  }
  
</script>
</body>
</html>

Credits

obniz developer team

obniz developer team

80 projects • 32 followers
Development board "obniz" is controlled via the internet.

Comments