Sending data from Processing to Arduino

From Interaction Station Wiki
Revision as of 10:38, 17 January 2023 by Annasa (talk | contribs) (Annasa moved page Writing-fromProcessing-toArduino to Sending data from Processing to Arduino)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Processing write to serial port

 1import processing.serial.*;
 2static final int PORT_INDEX=6,BAUDS=9600;
 3Serial myPort;
 4//Serial myPort;
 5void 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
13void draw(){
14if(mousePressed ==true){
15myPort.write('1');
16}else{
17myPort.write('0');
18}
19}

Arduino recive from serial port

 1#include <Adafruit_CircuitPlayground.h>
 2char val;
 3void setup(){
 4   Serial.begin(9600);
 5  CircuitPlayground.begin();
 6  }  
 7void 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}