Writing-fromProcessing-toArduino

From Interaction Station Wiki
Jump to: navigation, search

Processing write to serial port

  1. import processing.serial.*;
  2. static final int PORT_INDEX=6,BAUDS=9600;
  3. Serial myPort;
  4. //Serial myPort;
  5. void setup()
  6. {
  7.   size(400,400);
  8.   final String[] ports = Serial.list();
  9.   printArray(ports);
  10.   myPort =new Serial(this, ports[PORT_INDEX], BAUDS); 
  11. }
  12.  
  13. void draw(){
  14. if(mousePressed ==true){
  15. myPort.write('1');
  16. }else{
  17. myPort.write('0');
  18. }
  19. }

Arduino recive from serial port

  1. #include <Adafruit_CircuitPlayground.h>
  2. char val;
  3. void setup(){
  4.    Serial.begin(9600);
  5.   CircuitPlayground.begin();
  6.   }  
  7. void loop(){
  8.   CircuitPlayground.clearPixels();
  9.     if (Serial.available()){
  10.       val= Serial.read();
  11.       }
  12.    if(val=='1'){
  13.      for (int i=0;i<10;i++){
  14.         if (i!=0 && i!=2 && i!=4){
  15.           CircuitPlayground.setPixelColor(i, 255,   0,   0);
  16.             } 
  17.          }
  18.          delay(20);
  19.       }
  20.   if(val==0){
  21.       CircuitPlayground.clearPixels();  }
  22.       delay(10);  
  23. }