Send variables form Arduino to Processing
in
Integration and Hardware
•
2 years ago
Hello guys,
I'm working on a projekt and have a problem. I'm working with the Arduino board and Processing. I'm trying to make a SIMON Game: you have 4 leds and 4 switches. In the first round one led blinks if you push the right switch the next round starts and 2 leds blink and so on, if you fail you start from the beginning.
On the processing side i have a programm which starts the webcam from your computer and split the screen into 4 windows/screens (so you see yourself four times).
Okey, know I want that these screens change color when a led blinks (when the red led blinks one of the 4 windows gets colored red and so on) How can I make this? I have to send some variables from arduino to processing but I don't know how i should do this. Hope you understand what I mean and can help me.
PS: Sorry for my bad english. I'm not a native speaker.
Here is the arduino code:
I'm working on a projekt and have a problem. I'm working with the Arduino board and Processing. I'm trying to make a SIMON Game: you have 4 leds and 4 switches. In the first round one led blinks if you push the right switch the next round starts and 2 leds blink and so on, if you fail you start from the beginning.
On the processing side i have a programm which starts the webcam from your computer and split the screen into 4 windows/screens (so you see yourself four times).
Okey, know I want that these screens change color when a led blinks (when the red led blinks one of the 4 windows gets colored red and so on) How can I make this? I have to send some variables from arduino to processing but I don't know how i should do this. Hope you understand what I mean and can help me.
PS: Sorry for my bad english. I'm not a native speaker.
Here is the arduino code:
- int switch1 = 5; //schalter Pins
int switch2 = 4;
int switch3 = 3;
int switch4 = 2;
int led1 = 12; //LED Pins
int led2 = 10;
int led3 = 9;
int led4 = 8;
int turn = 0;
int input1 = LOW;
int input2 = LOW;
int input3 = LOW;
int input4 = LOW;
int randomArray[100];
int inputArray[100];
void setup() {
Serial.begin(9600);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(switch1, INPUT);
pinMode(switch2, INPUT);
pinMode(switch3, INPUT);
pinMode(switch4, INPUT);
randomSeed(analogRead(0)); //Added to generate "more randomness" with the randomArray for the output function
for (int y=0; y<=99; y++){ //For statement to loop through the output and input functions
output();
input();
}
}
void output() { //function for generating the array to be matched by the player
for (int y=turn; y <= turn; y++){ //Limited by the turn variable
Serial.println(""); //Some serial output to follow along
Serial.print("Turn: ");
Serial.print(y);
Serial.println("");
randomArray[y] = random(1, 5); //Assigning a random number (1-4) to the randomArray[y], y being the turn count
for (int x=0; x <= turn; x++){
Serial.print(randomArray[x]);
if (randomArray[x] == 1) { //if statements to display the stored values in the array
digitalWrite(led1, HIGH);
delay(500);
digitalWrite(led1, LOW);
delay(100);
}
if (randomArray[x] == 2) {
digitalWrite(led2, HIGH);
delay(500);
digitalWrite(led2, LOW);
delay(100);
}
if (randomArray[x] == 3) {
digitalWrite(led3, HIGH);
delay(500);
digitalWrite(led3, LOW);
delay(100);
}
if (randomArray[x] == 4) {
digitalWrite(led4, HIGH);
delay(500);
digitalWrite(led4, LOW);
delay(100);
}
}
}
}
void input() { //Function for allowing user input and checking input against the generated array
for (int x=0; x <= turn;){ //Statement controlled by turn count
input1 = digitalRead(switch1);
input2 = digitalRead(switch2);
input3 = digitalRead(switch3);
input4 = digitalRead(switch4);
if (input1 == HIGH){ //Checking for button push
digitalWrite(led1, HIGH);
delay(200);
digitalWrite(led1, LOW);
inputArray[x] = 1;
delay(50);
Serial.print(" ");
Serial.print(1);
if (inputArray[x] != randomArray[x]) { //Checks value input by user and checks it against
fail(); //the value in the same spot on the generated array
} //The fail function is called if it does not match
x++;
}
if (input2 == HIGH){
digitalWrite(led2, HIGH);
delay(200);
digitalWrite(led2, LOW);
inputArray[x] = 2;
delay(50);
Serial.print(" ");
Serial.print(2);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;
}
if (input3 == HIGH){
digitalWrite(led3, HIGH);
delay(200);
digitalWrite(led3, LOW);
inputArray[x] = 3;
delay(50);
Serial.print(" ");
Serial.print(3);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;
}
if (input4 == HIGH){
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led4, LOW);
inputArray[x] = 4;
delay(50);
Serial.print(" ");
Serial.print(4);
if (inputArray[x] != randomArray[x]) {
fail();
}
x++;
}
}
delay(500);
turn++; //Increments the turn count, also the last action before starting the output function over again
}
void fail() { //Function used if the player fails to match the sequence
for (int y=0; y<=5; y++){ //Flashes lights for failure
digitalWrite(led1, HIGH);
digitalWrite(led2, HIGH);
digitalWrite(led3, HIGH);
digitalWrite(led4, HIGH);
delay(200);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
digitalWrite(led3, LOW);
digitalWrite(led4, LOW);
delay(200);
}
delay(500);
turn = -1; //Resets turn value so the game starts over without need for a reset button
}
void loop() { //Unused void loop(), though for some reason it doesn't compile without this /shrug
}
- import processing.serial.*;
import JMyron.*;
Serial port;
JMyron m;//a camera object - //the four Images
PImage upperImage;
PImage lowerImage;
PImage urightImage; //upper right Image
PImage lrightImage; //lower right Image
void setup(){
size(800,800);
port = new Serial (this, "COM12", 9600);
m = new JMyron();//make a new instance of the object
m.start(width/2,height/2);//start a capture at 320x240
upperImage = new PImage(width/2,height/2);
urightImage = new PImage(width/2,height/2);
lrightImage = new PImage (width/2,height/2);
lowerImage = new PImage(width/2,height/2);
}
void draw(){
m.update();//aktuall image into the jmyron objekt
upperImage.loadPixels();
upperImage.pixels = m.image();
upperImage.updatePixels();
image (upperImage,0,0);
tint(255,0,0);
urightImage.loadPixels();
urightImage.pixels = m.image();
urightImage.updatePixels();
image (urightImage,400,0);
tint(255,255,0);
lowerImage.loadPixels();
lowerImage.pixels = m.image();
lowerImage.updatePixels();
image (lowerImage,0,400);
tint(0,0,255);
lrightImage.loadPixels();
lrightImage.pixels = m.image();
lrightImage.updatePixels();
image (lrightImage,400,400);
tint(0,255,0);
}
1