Processing to arduino
in
Integration and Hardware
•
9 months ago
For a project at school i have to make a program that connects processing to the arduino.
I want my processing sketch to send a signal (see incoming == 1 in my arduino sketch) if the dropping balls collide with the ellipses at the bottom.
Arduino code:
- float incoming;
- int ledPin8 = 8;
- int ledPin9 = 9;
- int ledPin10 = 10;
- int ledPin11 = 11;
- int ledPin12 = 12;
- int buttonState2 = 2;
- int buttonState3 = 3;
- int buttonState4 = 4;
- int buttonState5 = 5;
- int buttonState6 = 6;
- const int buttonPin2 = 2;
- const int buttonPin3 = 3;
- const int buttonPin4 = 4;
- const int buttonPin5 = 5;
- const int buttonPin6 = 6;
- void setup() {
- pinMode(ledPin8, OUTPUT);
- pinMode(ledPin9, OUTPUT);
- pinMode(ledPin10, OUTPUT);
- pinMode(ledPin11, OUTPUT);
- pinMode(ledPin12, OUTPUT);
- pinMode(buttonState2, INPUT);
- pinMode(buttonState3, INPUT);
- pinMode(buttonState4, INPUT);
- pinMode(buttonState5, INPUT);
- pinMode(buttonState6, INPUT);
- Serial.begin(9600);
- }
- void loop(){
- buttonState2 = digitalRead(buttonPin2);
- buttonState3 = digitalRead(buttonPin2);
- buttonState4 = digitalRead(buttonPin2);
- buttonState5 = digitalRead(buttonPin2);
- buttonState6 = digitalRead(buttonPin3);
- if(Serial.available() > 0){
- incoming = Serial.read();
- Serial.print(incoming,DEC);
- if (buttonState2 == HIGH && incoming == 1) { // add && check in processing for collision
- digitalWrite(ledPin8, HIGH);
- }
- if (buttonState3 == HIGH && incoming == 1) { // add && check in processing for collision
- digitalWrite(ledPin9, HIGH);
- }
- if (buttonState4 == HIGH && incoming == 1) { // add && check in processing for collision
- digitalWrite(ledPin10, HIGH);
- }
- if (buttonState5 == HIGH && incoming == 1) { // add && check in processing for collision
- digitalWrite(ledPin11, HIGH);
- }
- if (buttonState6 == HIGH && incoming == 1) { // add && check in processing for collision
- digitalWrite(ledPin12, HIGH);
- }
- }
- }
Processing code:
- import processing.serial.*;
- int[] sphereYCoords = { 0, 0, 0, 0, 0 };
- import processing.serial.*;
- Serial port;
- int val = 0;
- float sphere1Dropper;
- float sphere2Dropper;
- float sphere3Dropper;
- float sphere4Dropper;
- float sphere5Dropper;
- void setup(){
- size(450,1000);
- background(0);
- smooth();
- String portName = Serial.list()[0];
- port = new Serial(this, portName, 9600);
- }
- void draw(){
- beginShape();
- stroke(255);
- strokeWeight(1);
- line(90, 0, 90, 1900);
- line(180, 0, 180, 1900);
- line(270, 0, 270, 1900);
- line(360, 0, 360, 1900);
- line(0, 250, 450, 250);
- line(0, 500, 450, 500);
- line(0, 750, 450, 750);
- line(0, 1000, 450, 1000);
- endShape();
- sphere1Dropper();
- sphere2Dropper();
- sphere3Dropper();
- sphere4Dropper();
- sphere5Dropper();
- beginShape();
- fill(255);
- strokeWeight(2);
- stroke(51,204,0);
- ellipse(45, 875, 45, 45);
- stroke(255,0,0);
- ellipse(135, 875, 45, 45);
- stroke(255,255,0);
- ellipse(225, 875, 45, 45);
- stroke(0,0,255);
- ellipse(315, 875, 45, 45);
- stroke(153,102,0);
- ellipse(405, 875, 45, 45);
- endShape();
- }
- void sphere1Dropper()
- {
- stroke(51,204,0);
- fill(51,204,0);
- for (int i=0; i<5; i++)
- {
- ellipse(45, sphereYCoords[i]++, 30, 30);
- }
- }
- void sphere2Dropper()
- {
- stroke(255,0,0);
- fill(255,0,0);
- for (int i=0; i<5; i++)
- {
- ellipse(135, sphereYCoords[i]++, 30, 30);
- }
- }
- void sphere3Dropper()
- {
- stroke(255,255,0);
- fill(255,255,0);
- for (int i=0; i<5; i++)
- {
- ellipse(225, sphereYCoords[i]++, 30, 30);
- }
- }
- void sphere4Dropper()
- {
- stroke(0,0,255);
- fill(0,0,255);
- for (int i=0; i<5; i++)
- {
- ellipse(315, sphereYCoords[i]++, 30, 30);
- }
- }
- void sphere5Dropper()
- {
- stroke(153,102,0);
- fill(153,102,0);
- for (int i=0; i<5; i++)
- {
- ellipse(405, sphereYCoords[i]++, 30, 30);
- }
- }
2