LoginSignup
0
1

More than 5 years have passed since last update.

serial通信 arduino, processing

Posted at

1 byte

settings

pin 2 : SW (release:OFF / push:ON)
pin 3 : LED

setting.png

setting-photo.jpg

arduino > processing

serial_write_1byte_with_sw.ino
const int pin = 2;
const int pinLED = 3;
int stateSW;

void setup(){
  Serial.begin(9600);
  pinMode(pin, INPUT);
  pinMode(pinLED, OUTPUT);
  stateSW = 0;
}

void loop(){
  stateSW = digitalRead(pin);
  //Serial.println(stateSW);
  if(stateSW == 1){
    Serial.write(1);
    digitalWrite(pinLED,HIGH);
  }else{
    Serial.write(0);
    digitalWrite(pinLED,LOW);
  }
}
serial_read_1byte.pde
import processing.serial.*;

Serial myPort;       

//user definitions
String COM_PORT="/dev/tty.usbmodem1421"; //change this!
int myByte = 0;

void setup(){
  size(400,400);
  //printArray(Serial.list());
  //myPort = new Serial(this, Serial.list()[2], 9600);
  myPort = new Serial(this, COM_PORT, 9600);

}

void draw(){
  background(255);
  while (myPort.available() > 0) {
    myByte = myPort.read();
    println(myByte);

    if(myByte == 1){
      background(0);
      fill(255);
      ellipse(200, 200, 100, 100);
    }
  }
}

processing > arduino

serial_read_1byte_led.ino
const int pinLED = 3;
int readByte = 0;

void setup(){
  Serial.begin(9600);
  pinMode(pinLED, OUTPUT);
}

void loop(){
  if (Serial.available() > 0) {
    readByte = Serial.read();
    Serial.println(readByte, DEC);
    if(readByte == 1){
      digitalWrite(pinLED,HIGH);
    }else{
      digitalWrite(pinLED,LOW);
    }

  }
}
serial_write_1byte.pde
import processing.serial.*;

Serial myPort;       
String COM_PORT="/dev/tty.usbmodem1421"; //change this!
int value = 0;

void setup(){
  size(400,400);
  //printArray(Serial.list());
  //myPort = new Serial(this, Serial.list()[2], 9600);
  myPort = new Serial(this, COM_PORT, 9600);
}

void draw(){
  background(255);

  if (keyPressed) {
    if (key == 'b') {
      value = 1;
      background(0);
      fill(255);
      ellipse(200, 200, 100, 100);
    }
  }else{
    value = 0;
  }
  myPort.write(value);
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1