Led System Turn on/off

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.

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

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

1 Like

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

// 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;
 }
 
 */

1 Like