~画面のボタンを押したらLED点灯~
画面のボタンを押すと、スクーミーボードのLEDが消えます!
目次
int ledPin = 10;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
if(Serial.available() > 0){
char val = Serial.read();
if(val == 'h'){
digitalWrite(ledPin, HIGH);
}else if(val == 'l'){
digitalWrite(ledPin, LOW);
}
}
}
PBtn btn = new PBtn(50,50,100,50);
import processing.serial.*;
Serial myPort;
void setup() {
size(200, 200);
noStroke();
frameRate(10);
myPort = new Serial(this, Serial.list()[0], 9600);
btn.display(0);
}
void draw() {
}
void mousePressed(){
if(btn.areachk() == 1)
btn.display(1);
myPort.write('h');
}
void mouseReleased(){
if(btn.areachk() == 1)
btn.display(0);
myPort.write('l');
}
class PBtn {
int x, y, w, h;
PBtn (int xpos,int ypos,int wb,int hb) {
x = xpos;
y = ypos;
w = wb;
h = hb;
}
void display(int stat){
int col_top;
int col_right;
int col_bottom;
int col_left;
if(stat == 0){
col_top = 230;
col_right = 100;
col_bottom = 80;
col_left = 210;
}
else {
col_top = 80;
col_right = 210;
col_bottom = 230;
col_left = 100;
}
fill(255, 255, 0);
rect(x,y,w,h);
fill(col_top);
quad(x-10, y-10, x+w+10, y-10, x+w, y, x, y);
fill(col_right);
quad(x+w, y, x+w+10, y-10, x+w+10, y+h+10, x+w, y+h);
fill(col_bottom);
quad(x, y+h, x+w, y+h, x+w+10, y+h+10, x-10, y+h+10);
fill(col_left);
quad(x-10, y-10, x, y, x, y+h, x-10, y+h+10);
textSize(24);
fill(0);
text("PUSH",x+15, y+h+60);
text("LED OFF",x-0, y+h+80);
}
int areachk(){
if(mouseX > x && mouseX < x+w && mouseY > y && mouseY < y+h) return 1;
else return 0;
}
}
キャロット
このページのHelloNoは、050004 です!