Go Down

Topic: "Always on Sensor" (Read 732 times) previous topic - next topic

braddo_99

In my project, I would like to have several sensors always reading (two are accelometers and 4 are going to be RPM detectors with Hall sensors) so that I can periodically store temp values for dead reckoning later.

I have attempted to do this previously with pseudocode like below, and I find that it doesn't work because the variable in the second loop does not update. In this post, I'm mainly looking for a recommendation about high level approach, not code wrangling. Here's the pseudocode:

loop() {
Sense = read the sensor
if button pushed, Drive(setpoint)
}

Drive(int x){
while Sense != setpoint
run the motor
}

I solved it for the accels (with some help from Nick and WildBill - thanks!) by creating a read sensor function and calling this in both loops. That works because the accel reports an absolute value, but for the hall sensors, I have to make sure I don't miss any cycles or I will lose my dead reckoning and drift around. Maybe the sensing can bounce around in different loops and never lose track, but it sounds risky.

Anyway, I find that in the while loop above, the Sense variable value does not change. It's possible that I'm simply doing something else wrong, but I'm posting this to find out if the approach is even feasible, or what an alternative might be. I guess the main loop continuously reads the sensor until you jump out to the drive function, and then, since there's no re-reading going on (?) the motor never "reaches" the setpoint.

So i guess I'm a bit unclear on how the loops work. Does the main loop stop looping until the while returns, or do both the main loop and the while loop inside of Drive above run simultaneously in separate threads when the button is pushed. Of if not separate threads, do the loops run serially like so:

loop1
loop2
loop3
(button push)
while1
loop4
while2
loop5
etc

where the numbers are simply the times through the loops, not some explicit index.

Or instead is it like this:

loop1
loop2
loop3
(button pushed)
while1
while2
while3

This latter is what I'm seeing if I println variables in both loops. However, it's possible that I just had scoping problems or initializtation problems causing the variables not to update.

Sorry if this post seems obtuse or unclear. Happy to answer clarifying questions.


wildbill

it's like this:
Code: [Select]

loop1
loop2
loop3
(button pushed)
while1
while2
while3


Arduino is single threaded. So this:
Code: [Select]
Drive(int x){
while Sense != setpoint
run the motor
}

Needs to be
Code: [Select]

Drive(int x){
while Sense != setpoint
  {
  run the motor
  Sense = read the sensor
  }
}


The exception is when Sense is being updated in an interrupt routine.

Go Up