Pages: 1 [2]   Go Down
Author Topic: Basic If statement issue in a large program  (Read 1161 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Very true Nick, however "testing" will never be greater than 20,000 and millis() will always be greater at this point due to a 20 second delay to avoid such an issue.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19036
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are adding a 20 second delay to work around this problem? Hmmm. Why not just code it another way?

For example:

Code:
  if (millis() - testing >= 4000)

Your objective should be to write code that "always works". Not code that requires a 20-second delay at the start, and can't run for more than 3 hours, or some such limitations.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is the overall objective, but to try and sort out the strange issue of the If statement not working properly I have made it simple.

Once running properly I will use the millis() function to set the "testing" variable so it will be correct. The 20 second delay is actually for the external hardware, which is out of my control.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 495
Posts: 19036
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't know about the others, but I'm not interested in trying to work why code, that is obviously wrong, fails. I suggest you fix up that line and we can take it from there. I'm not asking you to re-code the whole thing.
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

That is a perfectly valid argument.

Code attached.

The code is now "if (timeout <= millis() - bulbAtimer)"

You'll notice that timeout = millis() just above it, and "bulbAtimer" is a random number generated between 3 and 10, then multiplied by 100 to give 3000 to 10000 (3 to 10 seconds).

* To_upload_3.ino (57.13 KB - downloaded 9 times.)
Logged

Cincinnati, OH
Offline Offline
God Member
*****
Karma: 49
Posts: 857
I'm not bossy...I just know what you should be doing.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I've put several print statements before line 323 and they are never executed. Am I missing something??
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've put several print statements before line 323 and they are never executed. Am I missing something??

Yes, you're missing about 23 logic chips and miles of wiring. This is the main problem I have, if I change the code so it doesn't require the feedback from the circuitry, the code which is left runs fine.

I will have to spend a few hours re-writing the code so it still has the error but doesn't require the circuitry. I have so far stripped out 2/3 of it, I'll have to remove a lot more by the looks of it.

The reason for so many IC's is because I need so many different inputs and outputs. It is not a simple system at all. Its only caught fire once.
Logged

Pittsburgh, PA, USA
Offline Offline
Faraday Member
**
Karma: 98
Posts: 4813
I learn a bit every time I visit the forum.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I've put several print statements before line 323 and they are never executed. Am I missing something??

Yes, you're missing about 23 logic chips and miles of wiring. This is the main problem I have, if I change the code so it doesn't require the feedback from the circuitry, the code which is left runs fine.

I will have to spend a few hours re-writing the code so it still has the error but doesn't require the circuitry. I have so far stripped out 2/3 of it, I'll have to remove a lot more by the looks of it.

The reason for so many IC's is because I need so many different inputs and outputs. It is not a simple system at all. Its only caught fire once.

Catching fire indicates really bad design unless that was the whole idea.

Time with unsigned values all time best formula:
if ( now - start_time >= wait_till ) { then it's time to do what's in here }
else { second prize is two weeks in Philadelphia };

You might try using one code task to every so often read the sensors and copy the readings to global variables. Then have the code that needs the feedback use the data in those variables, put it inside an if(data changed) { }.

Are you using shift registers as pin multipliers?




Logged

I find it harder to express logic in English than in Code.
Sometimes an example says more than many times as many words.

Offline Offline
Newbie
*
Karma: 0
Posts: 36
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Are you using shift registers as pin multipliers?

Yes, I have 5 of them for reading inputs (giving 40 inputs), 4 for outputs (giving 32 outputs).

I thought about the idea of using an interrupt to read/write the shift registers, but only certain ones need to be read/written at certain times, depending on the rest of the system.

The reading/writing of the shift registers works fantastically, it is clean and the timing is deliberately designed to add a tiny delay every cycle - I don't want a "perfect" output while soak testing an imperfect system. The tiny time delay while reading/writing to the shift registers, and the periodic calling of the Random function is all designed to vary the output pulses ever so slightly, in addition to a random addition from the random function, and a larger shift every time the LED brightness reaches a limit. The results are very close to a realistic encoder it is emulating.

The bitSet and bitClear allows me to change a bit in use, while leaving the unused bits alone.

I did think about using a SIO device, but this time decided to keep it simple as I know what I am doing with 7400 logic.

It caught fire because of bad design - or rather because I used 1/2W resistors while being impatient waiting for the 3W resistors to arrive to drive the opto's at 240V, which didn't work anyway as the opto's I were using were useless. Anyway its no fun if it doesn't catch fire or explode, and you've got to have fun with electronics! It teaches you respect for them.
Logged

Pages: 1 [2]   Go Up
Jump to: