Making a Game With Arduino and Processing -
i trying form 2 player game requires audio reflex visual. using littebits sound trigger sound input , littbits arduino connect computer. new , don't know how connect arduino processing , use input sound trigger effect score when black square appears.
here code in processing , sample arduino code have taken littlebits website , tried modify little.
thanks in advance!
float dice;
int playerone = 0; //player 1 score (left paddle) int playertwo = 0; //player 2 score (right paddle) boolean onewins = false; boolean twowins = false; void setup(){ size(500, 500); smooth(); nostroke(); framerate(2.5); } void draw() { background(255); showgui(); dice = random(0, 3); if (dice < 1.000001 && dice > 0.1){ fill ((0), (255), (0)); ellipse (250,250,100,100); } else if (dice < 2.000001 && dice > 1.000001){ rectmode(radius); fill ((255), (0), (0)); rect (250,250,50,50); } else if (dice < 3.000000 && dice > 1.000000){ rectmode(radius); fill ((0), (0), (255)); rect (250,250,50,50); } else if (dice < 0.1){ rectmode(radius); fill(0); rect(250,250,50,50); } } ----------arduino------ void setup() { serial.begin(9600); //establish rate of serial communication establishcontact(); //see function below } void loop() { if (serial.available() > 0) { int inbyte = serial.read(); int lefttrigger = analogread(a0); serial.print(lefttrigger, dec); serial.print(","); int righttrigger = analogread(a1); serial.println(righttrigger, dec); } } void establishcontact() { while (serial.available() <= 0) { serial.println("hello"); delay(300); } }
you need 2 pieces of code work: 1 on arduino sends commands, , 1 processing receive , parse commands.
i haven't used littlebits modules, here's button example this detailed tutorial.
arduino code:
int switchpin = 4; // switch connected pin 4 void setup() { pinmode(switchpin, input); // set pin 0 input serial.begin(9600); // start serial communication @ 9600bps } void loop() { if (digitalread(switchpin) == high) { // if switch on, serial.print(1, byte); // send 1 processing } else { // if switch not on, serial.print(0, byte); // send 0 processing } delay(100); // wait 100 milliseconds } and matching processing code:
import processing.serial.*; serial port; // create object serial class int val; // data received serial port void setup() { size(200, 200); framerate(10); // open port board connected // , use same speed (9600bps) port = new serial(this, 9600); } void draw() { if (0 < port.available()) { // if data available, val = port.read(); // read , store in val } background(255); // set background white if (val == 0) { // if serial value 0, fill(0); // set fill black } else { // if serial value not 0, fill(204); // set fill light gray } rect(50, 50, 100, 100); } notice arduino sends value processing looks , interprets. can @ physicalpixel example arduino ide example on sending data processing arduino.
Comments
Post a Comment