problem with commnunicating arduino with processing (both sending and receiving)
in
Integration and Hardware
•
1 years ago
hello everyone,
I'm now working on how to communicate arduino with processing in both way. means the processing can read value from arduino to trigger certain animation, and the arduino can trigger some events ( like led on) when the animation runs in a certain point.
I wrote two quick sketches in arduino and processing. it is a snake goes from left to the right side of the screen. when it hits the right side, it will stop, and the led of arduino will be on for a certain period (3 seconds). after that, the snake goes from left to right again, and the led should be off for the next round.
the problem of my sketch is, the led will light up once, but never turns off again. i tried everything I could yet it still wont work.
does anyone have the similar problem or can anyone help me out ?
====================this is code in processing=================
I'm now working on how to communicate arduino with processing in both way. means the processing can read value from arduino to trigger certain animation, and the arduino can trigger some events ( like led on) when the animation runs in a certain point.
I wrote two quick sketches in arduino and processing. it is a snake goes from left to the right side of the screen. when it hits the right side, it will stop, and the led of arduino will be on for a certain period (3 seconds). after that, the snake goes from left to right again, and the led should be off for the next round.
the problem of my sketch is, the led will light up once, but never turns off again. i tried everything I could yet it still wont work.
does anyone have the similar problem or can anyone help me out ?
====================this is code in arduino=================
- void setup()
- {
- Serial.begin(9600);
- pinMode(13, OUTPUT);
- }
- void loop() {
- int check;
- if (Serial.available()>0) {
- check = Serial.read();
- if(check == '0') {
- digitalWrite(13, HIGH);
- delay(3000);
- Serial.write('1');
- } else {
- digitalWrite(13, LOW);
- Serial.write('0');
- }
- }
- }
- import processing.opengl.*;
- import processing.serial.*;
- Serial myPort;
- int signal;
- Mover[] mover;
- int nummover = 7;
- int sta;
- void setup() {
- frameRate(30);
- size(1000,1000,OPENGL);
- smooth();
- background(0);
- println(Serial.list());
- myPort = new Serial(this, Serial.list()[0], 9600);
- mover = new Mover[nummover];
- for(int i=0; i<nummover; i++) {
- mover[i]=new Mover();
- }
- }
- void draw() {
- noStroke();
- background(255);
- for (int i=0; i<nummover; i++) {
- mover[i].update();
- mover[i].checkedge();
- mover[i].display();
- }
- if(mover[0].putAhold == true) {
- sta = '0';
- }
- else {
- sta = '3';
- }
- myPort.write(sta);
- println(sta);
- }
- void serialEvent(Serial myPort) {
- signal = myPort.read();
- // println(signal);
- if(signal == '1') {
- for (int i=0; i<nummover; i++) {
- mover[i].putAhold = false;
- }
- }
- }
- class Mover {
- PVector[] location;
- PVector velocity;
- PVector acc;
- float theta=random(360);
- float ra=6;
- float co=20;
- int tail = 250;
- boolean putAhold = false;
- Mover () {
- location = new PVector[tail];
- for (int i=0; i<tail; i++) {
- location[i] = new PVector(0,height/2);
- }
- velocity = new PVector(0,0);
- }
- void update() {
- acc = new PVector(ra,cos(radians(theta))*ra);
- acc.normalize();
- velocity.add(acc);
- if(putAhold == true) {
- velocity.limit(0);
- }
- if(putAhold == false) {
- velocity.limit(6);
- }
- for(int i=tail-2; i>=0; i--) {
- location[i+1].x = location[i].x;
- location[i+1].y = location[i].y;
- location[i].add(velocity);
- }
- theta += 5;
- }
- void checkedge() {
- for(int i=tail-2; i>=0; i--) {
- if (location[i].x>width) {
- location[i].x=0;
- putAhold = true;
- }
- }
- }
- void display() {
- for(int i=tail-2; i>=0; i--) {
- strokeWeight(5);
- stroke(0,(100-i*1));
- if(location[i+1].x - location[i].x < 2) {
- line(location[i].x, location[i].y, location[i+1].x, location[i+1].y);
- }
- }
- }
- }
1