We are about to switch to a new forum software. Until then we have removed the registration on this forum.
`int LedColor[][] = { { #ffcccd, #ff1924 }, // Red off and on
{ #bbffb5, #14ff00 }, //Green off and on
{ #fce9a9, #ffc400 } }; // Yellow off and on
int LabelVpos = 165;
String LedName[] = { "Active", "Green", "Yellow" };
int LedHpos[] = {60, 120, 180, 240, 320, 380};
int LedVpos = 140;
LEDs Led;
void setup() {
surface.setTitle("LED class test");
size(476, 300);
Led = new LEDs(0,"Red",LedColor[0],1,LedHpos[0],LedVpos,20,20);
}
void draw() {
delay(2000);
Led.LEDon();
delay(2000);
Led.LEDoff();
}
class LEDs {
String LEDname;
int LEDid;
int LEDcolor[]; // receiving an passed array
int LEDstat;
int Horiz;
int Vert;
int Wide;
int Tall;
LEDs(int A,String B,int C[],int D,int E,int F,int G,int H) {
LEDid = A;
LEDname = B;
LEDcolor = C; // LEDcolor[0]=off - LEDcolor[1]=on
LEDstat = D;
Horiz = E;
Vert = F;
Wide = G;
Tall = H;
if(LEDstat==1) fill(LEDcolor[1]); else fill(LEDcolor[0]);
ellipse(Horiz,Vert,Wide,Tall);
}
void LEDswitch() {
println("switch");
if(LEDstat==1) {
LEDstat=0;
fill(LEDcolor[0]);
ellipse(Horiz,Vert,Wide,Tall);
return;
}
LEDstat=1;
fill(LEDcolor[1]);
ellipse(Horiz,Vert,Wide,Tall);
}
void LEDon() { // Turn on LED
LEDstat=1;
fill(LEDcolor[1]);
ellipse(Horiz,Vert,Wide,Tall);
println("on");
}
void LEDoff() { // Turn off LED
LEDstat=0;
fill(LEDcolor[0]);
ellipse(Horiz,Vert,Wide,Tall);
println("off");
}
int LEDstatus() { return(LEDstat); } // Return LED status
}`
Answers
the Led.LEDswitch() method works as planned but the on and off methods don't :((
Please edit your post, select your code and hit ctrl+o in order to format your code.
Kf
No, edit the original post.
delay is bad and really shouldn't be used. read the reference.
https://processing.org/reference/delay_.html
also, the screen is only updated at the end of draw so you won't see the LEDon because it's overwritten by LEDoff before you get to see it and the delays are just wasting your time.
https://forum.processing.org/two/discussion/8085/i-display-images-in-sequence-but-i-see-only-the-last-one-why
but this might help:
https://forum.processing.org/two/discussion/8084/how-do-i-display-a-message-for-a-few-seconds#latest
(now, go back and fix the formatting of your code)
Thanks for taking a quick look... I only dropped the heinous delay() into the example code to show the issue. As per the site policy- I tried to give to most basic code that would replicate the issue. The LED methods all have an ellipse call in them and draw the LED as used. I have created other ways to call the methods in the class and they all work, but when I try from within the main draw() loop I get very inconsistent results. Example - I used keypressed call and called the methods directly.
Every time I look at the code I cut/pasted/edit I looks as it should. What formatting is needed ?
Select you code, control + o to indent, followed by leaving a linebreak before and after your code. That will do the job.
There is no keyPressed function in the provided code. It will be great to show your updated code if you are still having problems.
Regarding your original code at line 50, aren't you missing and else in your if statement?
Going back to your first question:
Kf
Tried testing without delay - using millis() Try using numeric keys - 123 turn on leds 456 turn them off and 789 swap off for on etc
but you have nothing in draw().
Don't need anything in draw for this test. It was just to use the keypressed to check the class - method functionality
As I previously stated, this is test code as per forum requirements.
If I understand correctly, if you call LEDon, it should keep the led ON for 2 secs. if you call the LEDoff...mmmm what should it do?
Now, if you want to time your led independently of each other, you need to create a member variable resetTime in your LEDs class. I show the concept next.
Now you need to set the proper reset time in draw and check for the time in order to do an action. Example:
.
This is the idea. I hope it helps,
Kf
Hi kfrajer Thanks for information, but that's not the issue. I have a lot more code that was calling the methods, actually it was coming in from several Arduinos over ethernet. This was a test code to emulate various on-board LED's on the Arduinos. My point was the methods all work perfectly when called from keypressed but when logic was placed in the draw() loop, with or without delay() and the methods were then called, they didn't work.
I have these Arduinos running various heating systems, recently I wanted to improve my coding and started to use classes, wrote my own timers and started to use threading etc, but this class with the LED methods keeps mucking me around. I tried re-writing the classes in a new sketch, simplifying the calling code, just to test. Thus the delay - turn on - delay - turn off - delay turn on etc. boring, but testing. Even tried on a different PC, same results. Then I double checked my class and methods by calling from external functions.
you posted new code but you didn't say whether the code is working as expected or not working as expected or what the expected behaviour was. how are we meant to know?
again, the display is only updated at the end of draw(). if you do two or more things to the same LED in the same call to draw() you will only see the last one.
you will only ever see the white circle.
Before rushing to tell me the same thing again, please try the code I posted. I realize what is good and what is bad code. I asked the forum because I have tried various different ways to test my class and methods and I can't fix the issue. Using the keypressed function, all the methods work and draw correctly. ONLY when I try testing calls from in the draw() loop do I get issues. The system runs the EXACT same methods as previously proven to work, but now don't.
You DO NOT use delay or any thing that does the same thing in draw. Use the answer provided by @kfrajer.