serial 통신 arduino, processing

1 byte



설정


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





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);
}

좋은 웹페이지 즐겨찾기