Showing buttons based on a message received by arduino

Hi,

Welcome to the community! :wink:

Remember that the draw function is being executed roughly 60 times per second. And each time, you are drawing a grey background (btw you could write it like this : background(150); by just giving the white value).

Drawing a background is equivalent to clearing the entire screen. So if you display your image inside of your receive function, it’s going to display that image on a single frame then next frame, you are going to clear your screen so the image disappear.

Just keep track of a boolean value received that is first set to false then whenever you receive a positive answer, put that variable to true. Then display your images in draw accordingly :

void draw(){
  background(150);
  
  if(received){
    image(led_green, 50, 50);
  }else{
    image(led_red, 50, 50);
  }
}

void receive(...){
  // ...
  if(message == "ack_on") received = true;
  if(message == "ack_off") received = false;
 // ...
}
1 Like