I could use some help with an Arduino counter program.

So I recently started this project that requires me to assign a counter to a continuously growing amount of "1"'s.

What I have so far is a number generator that generates the number "1" every 2-3 seconds. I can change the time from 2-3 seconds to a slower amount of time like 15 seconds but the number that is generated will always by "1".

What I'm trying to do is for every "1" that is generated, I have to assign a counter to that "1". For example, when the program first starts, a 1 will be generated, a counter (starting at 0) has to then be applied to this first "1". 2-3 seconds later, a second "1" will be generated, a new counter(which will also start at 0) will be assigned to this "1"and the previous counter for the original "1" will be incremented by one. Then 2-3 seconds after that, a third "1" will be generated, it will be given a counter (again, starting at 0) and the counter for the first "1" will go from 1 ->2 (due to it being incremented by one) while the counter for the second"1" will go from 0 -> 1 (also incremented) .

I think I have to use an array and a for loop within a for loop, but I'm kind of stumped, can anyone give me any advice or can point me in the right direction? I'm not asking for code because I should be able to do that part, I'm just having trouble with the logic right now. Perhaps a pseudo-code?

For reference, I have an Arduino Uno.

Any help is greatly appreciated!

Hi, all you are doing is counting 1's, and you are not giving them a unique value or time stamp, so why store them.

Just count 1's, and store the count, you now that the last one is going to be allocated "0" and the first is going to be allocated count-1.

I think you had better tell us the principle behind what you want to program and why.
There is probably an easier way, also what is your programming and electronics skills

Thanks, hope to help, Tom.... :slight_smile:

Hey TomGeorge,

The "1" that is generated is being generated by a pulse generator and it's supposed to symbolize a problem. The reason why i'm incrementing each "1" is because after a certain amount of incrementations, I want to output on the serial monitor something like "problem item 1 has reached an incrementation of 5, we are now going to deal with this problem".

Going back to my previous example (and replacing every "1" with the word problem):
For every "problem" that is generated, I have to assign a counter to that "problem". For example, when the program first starts, problem 1 will be generated, a counter (starting at 0) has to then be applied to problem 1. 2-3 seconds later, problem 2 will be generated, a new counter(which will also start at 0) will be assigned to problem 2 and the counter for problem 1 will be incremented by one. Then 2-3 seconds after that, a third problem will be generated, it will be given a counter (again, starting at 0) and the counter for the first problem will go from 1 ->2 (due to it being incremented by one) while the counter for the second problem will go from 0 -> 1 (because it was incremented).

Now let's say problem 1 reaches a special number for its counter, like 5 I want to then output a line saying something like "problem 1 has reached counter 5... we are going to deal with this problem now".

That's the whole gist of my situation.

As for my experience, I'm not that well versed in electronics, however I know the programming languages c and c++ relatively well (I'm just a little rusty!).

thanks again.

JandrewH:
Hey TomGeorge,

The "1" that is generated is being generated by a pulse generator and it's supposed to symbolize a problem. The reason why i'm incrementing each "1" is because after a certain amount of incrementations, I want to output on the serial monitor something like "problem item 1 has reached an incrementation of 5, we are now going to deal with this problem".

Instead of counting you could use an array to act like a FIFO shift register. Put item 1 in array index 0. When item 2 comes along move item 1 to index 1 and put item 2 into index 0. Each new item moves all the others along one place and is put in index O. When an item gets to index 4 (it's now 5th in the list) you output it to the serial monitor and, perhaps, move it to a new list of 'items currently being dealt with'.

Hi, because you are not discriminating between problems, a counter is all you need, when the counter reaches 5 you signal.
As you fix each problem you decrement the counter.
Why store 5 1's when the arduino doesn't know the difference between them.
All you are doing is counting problems, items, objects that are all the same to the arduino.

In other words you are counting problems, not storing what the problems are.

Tom..... :slight_smile:

Perhaps the simulation would be more realistic if, whenever an event happens you assign it a random value between (say) 1 and 5. The you count all the 1s into the 1 box, all the 2s into the 2 box etc.

The "boxes" could be variables named (eg) count1, count2 etc or they could be elements of an array such as countArray[1] countArray[2].

...R

Thanks for the advice everybody!

What I ultimately ended up doing was creating some arrays to store the problem items and then after a problem item reached the special value, it was reset so a new problem item could take its place.