Two chip sanity check

I've got two processes which are both time sensitive.

  1. Every cycle I loop for .25 sec and wait for pulses coming in via an interrupt. I take the count of the pulses and update a LCD.

  2. I continuously listen for signals coming in from an I.R. receiver and stop a timer if it's my I.R. beam coming in. I then update the same LCD

Does this require two chips? I'm using ATmega328s.

Thanks!

Nope. One processor can do an awful liot of stuff in that 250mS.

I don’t understand. Doesn’t my 250ms loop, within void loop(){}, block the processor from doing anything with the pin listening for the IR sensor?

Thanks!

Write your loop to check for IR pulses, or a flag set in the IR for the pulses. If the flag is set, update your counter, then go back to doing IR stuff.

I have a scoring machine that checks a series of things every 100uS - and most of the time nothing is going on, so it os very responsive when something does occur.

Are you using the delay() function? If so, then yes that can make doing two things "at once" difficult.

In many cases, we want loop() to run as fast as possible, checking for things to do, and not calling delay(). If for one of those things, we only want to check every 250ms, then get the elapsed time from millis() and save it in a variable. Then check the variable against millis() every time through loop(), and only do the 250ms thing if the current value of millis() is at least 250 more than the value saved in the variable. Then reset the variable to the new current millis() value.

See the famous example in the Arduino IDE, File > Examples > 2.Digital > BlinkWithoutDelay.

I better check my understanding of loop.

void loop(){

long startTime = millis(); //lets call this counting while(millis() - startTime < 250) { count something }

//lets call this blinking

if (ledState == LOW) ledState = HIGH; else ledState = LOW;

}

Am I correct in thinking that while counting is running for 250ms blinking is stopped or not running? So whatever the ledState is it will remain until counting is done?

Thanks!

Oh, okay guys.... Thanks. The dim light is coming on.

Can I: 1. use an interrupt on one pin to increment a counter 2. set the counter to 0 and startTime to millis() 3. monitor millis() in the loop for millis()- startTime > 250 4. grab the accumulated value of the counter and do something with it.

The reason is so my code can keep looping and doing other stuff.

Am I getting closer.

Thanks!

remout: Can I: 1. use an interrupt on one pin to increment a counter 2. set the counter to 0 and startTime to millis() 3. monitor millis() in the loop for millis()- startTime > 250 4. grab the accumulated value of the counter and do something with it.

Yes, yes, yes, and yes.

In your previous post, you are correct, while "count something" is running, nothing else is running. At the lowest level, the processor is only executing a single instruction at any given time. As we have only a single processor, it is physically impossible to run more than one instruction at a time. Any appearance to the contrary is just an illusion, caused by the speed of the processor and careful control of program flow and execution. Now if some interrupt is enabled, of course it is entirely possible that "count something" is interrupted, control transferred to the interrupt service routine, and then "count something" resumes when the ISR completes.

If you understand the "BlinkWithoutDelay" sketch, you will be well on your way. To test your comprehension, try writing a sketch that blinks two LEDs independently at different rates. Say, blink one once per second and another twice per second.

Note that there is some protocol to follow when sharing a variable between an interrupt service routine and the main program (loop). Because we only have an 8-bit processor, depending on the data type of the counter, it may take the main program more than one instruction to fetch it, zero it, etc. If the main program is in the middle of doing this when the interrupt fires, we can get invalid results. To avoid this, use the volatile qualifier when declaring the variable, and inhibit interrupts any time the variable is being accessed, updated, etc. by the main program. See the doc on the cli() and sei() functions.

You're getting the idea. You'll need to set a flag or something once startTime has begun so you don't reset the elapsed time at the top of loop every time.

Alternately, see attach and detach interrupts.

Thanks guys!

You’ve given me much to think about. Back to the drawing board.

Let us know how things work out. OTOH, going back to the title of this thread, I believe CrossRoads may actually have a multi-processor solution XD

Got it designed, still thinking about about other options before I plop down $25 for boards…
Take your pick, dual '328s or dual '1284s

How about adding mounting holes.

This more current layout has the mounting holes and additional ground pins that were discussed in an earlier thread.

I wrote these 2 small articles specifically on doing several things at the same time:

How to do several things at the same time

More on doing several things at the same time

@CrossRoads,

If your going to do it, do it BIG! Dual '1284s would totally rock! I probably would never get full use out of it but, it sure would be a neat little toy!

Okay, I’ll order some boards tonight. Was thinking about how to make a shield type board to go with it, what to do with all the pins that are there, which to bring up or to leave holes for, etc.

CrossRoads: Got it designed, still thinking about about other options before I plop down $25 for boards... Take your pick, dual '328s or dual '1284s

Hey crossroads, where do you order your pcbs from? Texy

My 2 orders have been with iteadstudio. Am waiting for mini-uino's to show up, status on 50-lot of 1.3" x 1.3" PCBs is "departure from outward office of exchange" on 9/4. Prior order for Bobuino's took a while too. http://iteadstudio.com/store/index.php?main_page=product_info&cPath=19_20&products_id=175&zenid=edplpd8kf14ntqn128uncg0bd6 and http://iteadstudio.com/store/index.php?main_page=product_info&cPath=19_20&products_id=173

seeedstudio offers similar http://www.seeedstudio.com/depot/fusion-pcb-service-p-835.html?cPath=185