Processing-i ulanyp PC we Arduino-ny habarlaşdyrma

used products:

1. Arduino UNO

content:

Sep: https://www.processing.org/tutorials/electronics/

Arduino:

--------------------------------------------------- --------------------------------------------------- --------------------------------------------------- Arduino Code: *************************************************** int switchPin = 6; int ledPin = 13; char val; int switchPinState; void setup() { pinMode(ledPin, OUTPUT); pinMode(switchPin, INPUT); Serial.begin(9600); digitalWrite(ledPin, LOW); } void loop() { if (Serial.available()) { val = Serial.read(); } if (val == 'H') { digitalWrite(ledPin, 1); } else { digitalWrite(ledPin, 0); } if (digitalRead(switchPin) == HIGH) { if (switchPinState == 0) { switchPinState = 1; Serial.print('1'); } } else { if (switchPinState == 1) { switchPinState = 0; Serial.print('0'); } } delay(1); } --------------------------------------------------- --------------------------------------------------- --------------------------------------------------- Processing Code: *************************************************** import processing.serial.*; CheckBox box = new CheckBox("Ýak", 300, 100, 200, 200); Serial port; int val; boolean b = false; void setup() { size(800, 400); //frameRate(300); String arduinoPort = Serial.list()[0]; arduinoPort = Serial.list()[Serial.list().length-1]; //"/dev/tty.usbmodem1411"; port = new Serial(this ,arduinoPort , 9600); } void draw() { update(); if (0 < port.available()) { val = port.read(); } char c = char(val); color colour = color(255,0,255); if (c == '0') { colour = color(185,111,10); background(colour); } else if (c == '1') { colour = color(90, 110, 170); background(colour); } box.display(); } void update() { if (overRect(box.getX(), box.getY(), box.getW(), box.getH()) == true) { box.setMouseState(CheckBox.STATE_OVER); } else { box.setMouseState(CheckBox.STATE_NORMAL); } } boolean overRect(int x, int y, int w, int h) { if ((mouseX >= x) && (mouseX <= x+w) && (mouseY >= y) && (mouseY <= y+h)) { return true; } else { return false; } } void mouseReleased() { if (box.getMouseState() == CheckBox.STATE_OVER) { if (box.isChecked()) { box.setTitle("Ýak"); box.setChecked(false); port.write('L'); } else { box.setTitle("Söndür"); box.setChecked(true); port.write('H'); } } } Processing Code (CheckBox Class): *************************************************** public class CheckBox { public static final int STATE_NORMAL = 1; public static final int STATE_OVER = 2; public static final int STATE_CHECKED = 3; private String title; private int x; private int y; private int w; private int h; private color colour; private int mouseState; private boolean checked; public CheckBox(String title, int x, int y, int w, int h) { this.title = title; this.x = x; this.y = y; this.w = w; this.h = h; setMouseState(STATE_NORMAL); setChecked(false); } public void display() { fill(colour); rect(this.x, this.y, this.w, this.h, 7); fill(0); textSize(34); text(this.title, this.x+40, this.y+110); } public String getTitle() { return this.title; } public void setTitle(String title) { this.title = title; } public int getMouseState() { return this.mouseState; } public void setMouseState(int mouseState) { this.mouseState = mouseState; if (checked) { return; } switch (mouseState) { case STATE_NORMAL: setColour(color(212)); cursor(ARROW); break; case STATE_OVER: setColour(color(235)); cursor(HAND); break; } } public boolean isChecked() { return this.checked; } public void setChecked(boolean checked) { this.checked = checked; if (checked) { setColour(color(247,239,0)); } else { setColour(color(212)); } } public color getColour() { return this.colour; } public void setColour(color colour) { this.colour = colour; } public int getX() { return this.x; } public void setX(int x) { this.x = x; } public int getY() { return this.y; } public void setY(int y) { this.y = y; } public int getW() { return this.w; } public void setW(int w) { this.w = w; } public int getH() { return this.h; } public void setH(int h) { this.h = h; } }