Go Down

Topic: Two chip sanity check (Read 7260 times) previous topic - next topic

remout

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!


CrossRoads

Nope.  One processor can do an awful liot of stuff in that 250mS.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

remout

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!

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

JChristensen

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.

remout

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!

remout

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!

JChristensen


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.

CrossRoads

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.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

CrossRoads

Alternately, see attach and detach interrupts.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

remout

Thanks guys!

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

JChristensen

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

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
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

JoeO

How about adding mounting holes.

CrossRoads

This more current layout has the mounting holes and additional ground pins that were discussed in an earlier thread.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up