For this lab assignment I wanted to create a game using both the Arduino board and Processing. I created the casino game roulette. A potentiometer acts as the on/off switch for the wheel. Bets are only allowed when the wheel is not spinning or has stopped.
Arduino code:
int potPin = 0; // select the input pin for the potentiometer
int motorPin = 9; // select the pin for the Motor
int valPot = 0; // variable to store the value coming from the sensor
int i = 255;
int j = 1;
long randomN;
void setup() {
Serial.begin(9600);
}
void loop() {
randomN = random(500, 1000);
valPot = analogRead(potPin); // read the value from the sensor, between 0 - 1024
if(valPot > 200){
j = 1;
}
else{
j = 0;
}
if(j == 1){
j = 0;
Serial.println(valPot);
Serial.println(randomN);
analogWrite(motorPin, i); // analogWrite can be between 0-255
Serial.write(analogRead(potPin));
Serial.println(i);
//delay(2000);
}
else{
analogWrite(motorPin, 0); // analogWrite can be between 0-255
}
}
Processing code
import processing.serial.*;
Serial port;
PImage imgChip; // Declare variable "a" of type PImage
PImage imgBackground;
PFont font; //declare font variable
int i = 0;
int pressValue = 0;
void setup() {
size(1461, 746);
println(Serial.list());
port = new Serial(this, Serial.list()[1], 9600);
fill(126);
imgBackground = loadImage("roulette-tafel.jpg"); //set background image
imgChip = loadImage("chip.png");
image(imgBackground,0,0); //desplay background image
//noLoop();
}
void draw() {
while (port.available() > 0) {
pressValue = port.read();
println("press "+pressValue);
}
}
void mousePressed() {
i = i+1; //used for bet amount
if(pressValue > 0){
fill(255,255,255);
text("No more bets", 10, 650, 700, 100); //amount
}
else{
noStroke();
fill(90,160,100);
rect(10,650,300,50);// erase previous num
stroke(0);
noStroke();
fill(90,160,100);
rect(1200,650,700,100);// erase previous num
stroke(0);
image(imgChip, mouseX-38, mouseY-38); //display chip
textSize(50);
fill(255,255,255);
text("BET: $" + i, 1200, 700, 300, 100); //amount
}
}
void keyPressed() {
background(imgBackground); //erase screen for new round
pressValue = 0; //reset serial value
i = 0; //reset counter
}