Ragooman
Posts: 13
Joined: Sun Jun 26, 2016 11:19 pm

Processing: Problem with Blink LED test using Shapes & Mouse

Sun Jun 26, 2016 11:33 pm

Hello,
I hope this is the correct section to post this problem. This is my first time using Processing on the Raspberry PI. So I made the traditional 'Blink the LED' test in Processing and it works fine. I connected a Red and Yellow LED on the breadboard to create a "ping-pong" type display. I'm using an earlier RPi controller, B+ 700mhz with the latest version of Jessie installed.

In this version the mouse click is a toggle function. I click the mouse once to start the LEDs blinking. I click the mouse button again, and the LEDs stop blinking. But I noticed something when I started adding graphics into the code. I added 2 shapes to represent the LED's, one is a red box and the other is a yellow box. And I have the code written to 'turn' on the shapes by changing the color whenever the LED on the breadboard is turned ON. Then when the LED is turned OFF, I change the color of the box to black.

But there seems to be some lag in displaying the graphics. Basically the LED on the breadboard will turn on immediately and the box in the window will change color about 1 second later. At first I thought maybe the "GPIO.digitalWrite" instruction is taking too long. But I swapped the code so the "fill" and "rect" instruction is done first before the "GPIO.digitalWrite" instruction is executed. But there was no difference or improvement.

The moment I click the mouse button to stop the blinking, the LEDs stop blinking immediately. Then there's a 1 second lag and then finally the box on the screen is updated. As you can see in the code, I placed the delay at the end of the draw function which is the most obvious place

So with the current state of the code [below] If I toggle the mouse button, once to start the blinking and then quickly click the mouse button again to stop the blinking once I see the LEDs change. So then it only loops once. I will see the LEDs immediately change first before the boxes on the screen is updated. And this lag can be as long as 1 second.

It's almost as if there was some mysterious delay function between these two lines of code in my program.

Code: Select all

rect(50, 20, 30, 30, 7);
GPIO.digitalWrite(redLEDPin, true);
I can't verify this on my desktop to see if I can reproduce the problem as I don't have any GPIO's available at the moment.

Has anyone noticed this before ?
Would I have something out of sequence in the code ?
Would the delay need to go somewhere else ?

Do you think my Raspberry PI is too slow. BTW, the IDE used in Processing is terribly slow on this. I suppose it would run better on a RPi2 or 3.

So what I do instead is write and build the code on my desktop using the Raspberry PI libraries, export the files to LinuxArmv6 platform, and then FTP those files to the PI and run it on there directly.

Would somebody be able to try this little Blinking LED test on their RPi and compare their results.
below is my test code

Code: Select all

//==================================================
// LED Blink test
//==================================================
// Import hardware IO library.
import processing.io.*;
 
// Pin #'s for LEDs and Button:
int redLEDPin   = 22;
int yelLEDPin = 27;
int count = 0;
int count2 = 0;
int i = 1;
 
// LED state, on or off (true or false).
boolean redLED = true;
boolean yelLED = false;
boolean mouseclk = false;
 
//----------------------------------------------------
void setup() 
{
  size(300, 200);
  textSize(10);
 
  // Initialize LEDs as outputs.
  GPIO.pinMode(redLEDPin, GPIO.OUTPUT);
  GPIO.pinMode(yelLEDPin, GPIO.OUTPUT);
 
  // Default to drawing black lines around buttons.
  stroke(0, 0, 0);
   
  // Turn the LEDs off.
  GPIO.digitalWrite(redLEDPin, false);
  GPIO.digitalWrite(yelLEDPin, false);
}
 
//--------------------------------------------------------
void draw()
{
  //check flag, only do this once in beginning
  if (count == 0)
  {
    background(0, 0, 255);
    fill(255,255,255);
    text("BEGIN TEST:",5,10);
    text("WAIT FOR MOUSE CLICK",150, 10);
    count = 1;
  }
   
  //use mouse button as toggle to start/stop blinking
  if (mouseclk)                      
  {
    background(0, 0, 255);
    fill(255,255,255);
    text("BEGIN TEST:",5,10);
    text("WAIT FOR MOUSE CLICK",150, 10);
    text("TEST LOOP# " + i + " ",150,100);
 
    //blink
    redLEDicon();
    yelLEDicon();
    redLED = !redLED;
    yelLED = !yelLED;
    i++;
    delay(1000);
  }
  else
  {
    fill(255,255,255);
    text("END TEST:",150,150);
  }
}
 
//------------------------------------------------------
void mousePressed() 
{
  //count2 = 1;
  //i = 1;
  //redLED = true;
  //yelLED = false;
  mouseclk = !mouseclk;
}
 
//----------------------------------------------------
void redLEDicon()
{
  if (redLED)
  {
    fill(255, 0, 0);
    text("Red=ON",50, 80);
    rect(50, 20, 30, 30, 7);
    GPIO.digitalWrite(redLEDPin, true);
  }
  else
  {
    fill(255, 0, 0);
    text("Red=OFF",50, 80);
    fill(0, 0, 0);
    rect(50, 20, 30, 30, 7);
    GPIO.digitalWrite(redLEDPin, false);
  }
}
 
//----------------------------------------------------
void yelLEDicon()
{
  if (yelLED)
  {
    fill(255, 255, 0);
    text("Yel=ON",200, 80);
    rect(200, 20, 30, 30, 7);
    GPIO.digitalWrite(yelLEDPin, true);
  }
  else
  {
    fill(255, 255, 0);
    text("Yel=OFF",200, 80);
    fill(0, 0, 0);
    rect(200, 20, 30, 30, 7);
    GPIO.digitalWrite(yelLEDPin, false);
  }
}
//=====================================================

Return to “Java”