Hi folks.
It has been a while.
Up front I shall say I apologise for the stupid questions I am about to ask. Something is really wrong with me and it seems what I once knew and thought worked, doesn't any more. Which is obvious in saying, but back then, it wasn't and now I am stuck with these problems which are because I misunderstood something at the start.
Background:
I was thrown in to Arduino head first a few years back and really had a hard time learning.
Along the way I did learn things and have written a couple of good sketches which do for me what I wanted/needed.
Sure along the way other people DID help me and wrote sketches. I won't deny that. I tried to use their sketches as foundations for new sketches and for most of the times: They worked.
But then it all fell apart and I am in a mess.
I would include the sketches here/now but really think it would do more harm than good.
One sketch which works and is good for the purpose written is a "strobe" sketch to pulse an output pin at a given frequency.
The frequency is set via the serial input. Not exactly rocket science. But my original attempt was ..... not really working.
The new sketch was good and as I said: It worked.
I used that to help me write a new sketch which was a bit different, in that instead of frequency, it was more Pulse Width Modulation.
Now, yeah, I know there are pins on the Arduino to do PWM. Why not use them?
Well, I wanted to practice programming and try to do it myself.
As it was supposed to be a SIMPLE program, I didn't really bother with making function blocks. There was no need. It simply went "top to bottom" and looped.
But then the Gremlins appeared and everything went pear shaped.
Adding debug code was messy and I thought I would put them in blocks and access them with simple calls. NOT as simple as I thought.
Because most things were defined in the LOOP() part of the code, their names were lost when I called the debug parts. Yeah, ok, I could have parsed them but that was only adding to overhead for me and further complicating things.
I persisted and got the code "working". But there were still bugs. What was my next step to be?
What I thought I would/cold do: Is take the variable names and put them before the SETUP() and LOOP() parts of the code. That way they would be GLOBAL names. Can't be that difficult can it?
Well it seems it can be.
I had a "working" version of the code.
I took ONE line:
unsigned long BLAH = ;
and put that OUTSIDE the LOOP() part of the code.
It compiled ok and I sent it to the Arduino.
The sketch didn't work.
I moved the line back WITHIN the LOOP() code, compiled it and uploaded it.
It worked.
I gave up. Yeah, not the best solution, but I had had it. I honestly couldn't work out what was going on.
Also, reading other people's sketches there are weird things like:
constant int X = 1;
I understood it was:
static int X = 1;
(outside LOOP() or any other blocks of code.)
And so I learned that I was dumber than I thought. I'm getting used to that these days.
Now I am trying to learn about loops.
The strobe sketch was easy enough to read and it was basically:
calculate X;
turn on output;
get micros();
store it;
if stored value + X > micros()
{
turn off output
}
BASICALLY!!!!
So I set up the variables, got the current micros(), sotred it, did the maths, and all that.
It seemed to work. But then the subtleties started to show up.
I was adjusting the step value by 100. I then put in code to allow adjustment of the step value to 10 or 50 or 100.
Things didn't add up.
Not having a CRO didn't help.
Finally I used a CRO and it was confirmed! It wasn't doing what it was supposed to do.
I reluctantly did a basic sketch:
every 2 seconds:
turn light on for 1 second.
repeat.
So it would:
read mircos()
trun on the light
add 1000000 and see if 1 second had passed.
If it had, turn the light off.
repeat.
It doesn't work!
I can't seem to get the code right.
Attached is the pitiful effort.
The other sketches won't be attached as I don't need that much salt to be rubbed in just now.
I HOPE to hear from people and HOPE to respond quickly.
Alas there is this ongoing problem where I am not getting notifications of replies.
That is why I have been so quite of late.
No good posting if you don't get told there are replies.
P,.S.
I will also include the strobe sketch. Not saying anything is wrong with it, but just so you can see how messed up I am at reading/understanding what is going on.
Strobe_2.ino (1.31 KB)
Another_LED_Test.ino (938 Bytes)