Variablen/Daten von Arduino an Processing senden

vor 34 weeks 4 days von EricDraven

Hallo Leute,
arbeite gerade an einem kleinen Spiel. Und zwar hab ich 4 Leds und 4 Schalter. In der ersten runde leuchtet eine Led auf, drückt man den richtigen Schalter dazu, leuchten in der nächsten Runde zwei Leds und man muss wieder die dazugehörigen schalter drücken usw. bis man sich verdrückt dann fängt das Spiel von vorne an. Soweit sogut das funktioniert auch alles prima.
Ich möchte nun Variablen von Arduino auf Processing senden. Auf Processing hab ich was programmiert das, die Webcam eingeschaltet wird und der Screen in 4 Teile geteilt wird (d.h. man sieht sich dann 4 mal). Nun möchte ich wenn eine Led leuchtet bzw. auch wenn der Schalter zu der Led gedrückt wird das ein Teil in der entsprechenden Led farbe aufleuchtet. Mein Problem is nur das ich keine Ahnung hab wie ich jetzt daten von Arduino an Processing sende. Hoffe ihr könnt mir Helfen.

Hier mal der 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
}

Und hier der Processing Code:

import processing.serial.*;
import JMyron.*;

Serial port;
JMyron m;//a camera object

PImage upperImage;
PImage lowerImage;
PImage urightImage;
PImage lrightImage;

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();//Aktuelles Bild ins Myron Objekt

//Schreiben des Kamerbildes in den Buffer und darstellen in oberer Bildschirmhälfte
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);

}

Ähnliche Posts

1 Antwort auf “Variablen/Daten von Arduino an Processing senden”


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
  1. Spot sagt:

    Hallo

    So eine etwas verspätete Antwort aber vielleicht hilft sie ja noch.

    Du hast ja in dem PC Programm schon eine RS232 initialisiert nur soweit ich das gesehen habe benutzt du sie nicht. Wenn die Arduino IDE läuft greift sie auf die RS232 zu und ein Programm kann sie nicht benutzen. Du musst also die Verbindung zwischen der Arduino und "seinem" PC Programm trennen und dann mit deinem PC Programm die Verbindung öffnen, dann erhält dein Programm die Meldungen die du jetzt im Serial Monitor siehst. Diese Meldungen bearbeitest du und schon hast du das was du willst.

    mfg
    Spot

    Login or register to post comments