# Led System Turn on/off

**URL:** https://discourse.processing.org/t/led-system-turn-on-off/12662
**Category:** Electronics (Arduino, etc.)
**Created:** [July 11, 2019, 12:22pm UTC](https://discourse.processing.org/t/led-system-turn-on-off/12662 "2019-07-11T12:22:54Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Murphy](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Murphy](https://discourse.processing.org/u/Murphy)
#### Post date: [July 11, 2019, 12:22pm UTC](https://discourse.processing.org/t/led-system-turn-on-off/12662/1 "2019-07-11T12:22:54Z")

</div>

Hey everyone,  
I am new. I want to do led system. I do all. I need just something. Firstly, I want to put 2 button on Processing. One button work to start light. One button work to stop light. I tried 10 time but I can’t do it. I need help for this topic. I hope so you can help me. Thanks for reading. I am sharing my codes.

```auto
import processing.serial.*;

Serial myPort;

int value = 0;
int d = day(); // Values from 1 - 31
int m = month(); // Values from 1 - 12
int y = year(); // 2003, 2004, 2005, etc.

void setup(){
  
  myPort=new Serial(this, "COM3", 9600);
  
  size(400, 310);

  background(255, 0, 0);
  
  fill(0, 0, 0);
  text("LED CONTROL", 145, 40);
  
  fill(0, 0, 0);
  text("/", 356, 300);

  fill(0, 0, 0);
  text("/", 366, 300);
  
  String s = String.valueOf(d);
  text(s, 342, 300);
  s = String.valueOf(m);
  text(s, 360, 300); 
  s = String.valueOf(y);
  text(s, 370, 300);
}

void draw(){
  
  ****if(mouseClicked(text("START")){** /// I think problem is here**
****myPort.write('1');****
 ****}****
****if(mouseClicked(text("STOP")){** /// I think problem is here**
****myPort.write('0');****
  }
  fill(value);
  rect(140, 60, 95, 95);
  stroke(0, 0, 0);
  fill(0, 0, 0);
  
  fill(value);
  rect(140, 180, 95, 95);
  
  stroke(0, 0, 0);
  fill(0, 0, 0);

  fill(255, 255, 255);
  text("START", 167, 110);
  
  fill(255, 255, 255);
  text("STOP", 170, 230);
}

void mouseClicked() {
  if (value == 0){
    value = 50;
  } else {
    value = 0;
  }
}

```

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 11, 2019, 1:38pm UTC](https://discourse.processing.org/t/led-system-turn-on-off/12662/2 "2019-07-11T13:38:01Z")

</div>

can you split this into 2 modules,  
-a- make a toggle button in processing  
-b- communicate with arduino and send at CHANGE.

for the button could use a library CP5 or G4P  
or better learn to make your own…

a toggle button can be

- a rectangle
- and a text
- a mouse over rectangle ( boolean ) function combined with the
- [https://processing.org/reference/mousePressed\_.html](https://processing.org/reference/mousePressed_.html)
- a button status boolean and its reversed SETPOINT ( ON , OFF ) to show as text

---

<div class="post-metadata">

### Author: ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)
#### Post date: [July 12, 2019, 11:59am UTC](https://discourse.processing.org/t/led-system-turn-on-off/12662/3 "2019-07-12T11:59:32Z")

</div>

ok, i not know your project status,  
but i think it’s worth to have a easy  
TOGGLE button code ready.

```auto
// Basic Button Example / TOGGLE Version
// _________________________________________________________________ SETUP
void setup() {
  size(200, 200);
}

// _________________________________________________________________ DRAW
void draw() {
  background(200, 200, 0);
  myButton();
}

// _________________________________________________________________ MOUSE pressed function
void mousePressed() {
  left_mouse_click_button();
}

// _________________________________________________________________ YOUR code if button pressed
void user_on_button_click() {
  bV = ! bV; // TOGGLE
  println("button PV "+bV);
  println("?what you want me to do now?");
}

// _________________________________________________________________ BUTTON ?TAB?
int bx=20, by=170, bw=60, bh=20; // button rectangle settings X,Y,W,H
int bts=15; // button text size
color bs = color(0, 50, 150); // stroke 
color bf0 = color(0, 0, 200); // PV fill STOP 
color bf1 = color(0, 200, 0); // PV fill RUN
color cshow = bf0; // selected PV color
color bt = color(200, 200, 0); // text
boolean benable=true; // false; // use button?
boolean bV = false; // true; // button status                       
String bT0 = "START"; // SP Text
String bT1 = "STOP"; // SP Text
String tshow = ""; // selected SP text temp

void myButton() {
  if (benable) {
    push();
    stroke(bs);
    strokeWeight(2);
    cshow = ( bV) ? bf1 : bf0;
    fill(cshow); // same if ( bV ) fill(bf1); else fill(bf0);
    rect(bx, by, bw, bh);
    fill(bt);
    textSize(bts);
    textAlign(CENTER);
    tshow = ( bV ) ? bT1 : bT0; // same if ( bV ) tshow = bT1; else tshow = bT0;
    text(tshow, bx+bw/2, by+bh/2+bts/2);
    pop();
  }
}

// _________________________________________________________________ mouse click left on button
void left_mouse_click_button() {
  if (mouseButton == LEFT && overRect(bx, by, bw, bh) && benable ) user_on_button_click();
}

// _________________________________________________________________ mouse over rectangle yes/no
boolean overRect(int rx, int ry, int rwidth, int rheight) {
  if (mouseX >= rx && mouseX <= rx+rwidth && 
    mouseY >= ry && mouseY <= ry+rheight) return true;
  return false;
}

/*
// Basic Button Example
 // _________________________________________________________________ SETUP
 void setup() {
 size(200, 200);
 }
 
 // _________________________________________________________________ DRAW
 void draw() {
 background(200, 200, 0);
 myButton();
 }
 
 // _________________________________________________________________ MOUSE pressed function
 void mousePressed() {
 left_mouse_click_button();
 }
 
 // _________________________________________________________________ YOUR code if button pressed
 void user_on_button_click() {
 println("what you want me to do now?");
 }
 
 // _________________________________________________________________ BUTTON ?TAB?
 int bx=20, by=170, bw=60, bh=20; // button rectangle settings X,Y,W,H
 color bs = color(0, 50, 150); // stroke 
 color bf = color(0, 200, 200); // fill
 color bt = color(0, 0, 200); // text
 boolean benable=true; // false; // use button?
 String bT = "click me"; // Text
 
 void myButton() {
 if (benable) {
 push();
 stroke(bs);
 strokeWeight(2);
 fill(bf);
 rect(bx, by, bw, bh);
 fill(bt);
 text(bT, bx+5, by+15);
 pop();
 }
 }
 
 // _________________________________________________________________ mouse click left on button
 void left_mouse_click_button() {
 if (mouseButton == LEFT && overRect(bx, by, bw, bh) && benable ) user_on_button_click();
 }
 
 // _________________________________________________________________ mouse over rectangle yes/no
 boolean overRect(int rx, int ry, int rwidth, int rheight) {
 if (mouseX >= rx && mouseX <= rx+rwidth && 
 mouseY >= ry && mouseY <= ry+rheight) return true;
 return false;
 }
 
 */

```
