Go Down

Topic: cannot seem to set up analog read variables in setup() (Read 585 times) previous topic - next topic

vagittarius

Hello,

What I am trying to accomplish is this: I have an analog sensor (a stretch sensor, but it returns values like a potentiometer) which must control servos. Since the stretch sensor changes resistance over time and use, I need to set up my minimum and maximum variables at the outset of my program. I have a button connected as well as an indicator LED for this purpose. I want to be able to
1. turn on the Arduino, enter setup(),
2. wait for the button push,
3. when the button is pushed, set minimum variable to whatever is read by the sensor,
4. blink LED to indicate that is set,
5. wait for the button to be pushed again,
6. when it is pushed a second time, set the maximum variable to the sensor reading (it has been stretched to full range now),
7. blink LED again to indicate this is set,
8. begin the loop(), in which the servos' positions are mapped from 0 to 180 degrees based on the sensor input, using minimum and maximum which were determined in setup().

None of my for or while loops seem to work in setup(), and I cannot figure out why. Is it possible to create loops that wait for a button push in setup()?

Thank you for any help.

Chuck

Graynomad

Quote
None of my for or while loops seem to work in setup()

I would look closely at line 4 (hint == not =) and you have a ; at the end of the for loop on line 35.

Of course I'm just guessing because I can't see your code :)
______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

James C4S


I would look closely at line 4 (hint == not =) and you have a ; at the end of the for loop on line 35.

I scrolled up and down twice looking for the code I missed... ;)
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

thebfs

wow me to i thought for a sec somethig was wrong with my browser.
We got to see some code to give u much help.
why does your loops/ and configuration have to run in setup.
could create a initialize/config function u call  useing an if statment and is never called until reset in loop. try moveing it around.
or u dont need afunction

if (setRangeAlreadyRun == False){
  //check you max /min values and get it all set here

   setrange.... = true;
}

  //then run your other code
reserve setup for initialize var nameing etc.
i think it works better but not alwasy obviously.
but if what u did doesnt work try something like that

vagittarius

Thanks, that's a good idea, I'll try that.

Sorry, there isn't much code to see, because I kept trying different expressions and erasing them in favor of others when I couldn't get them to work. What I experienced was every for loop or switch expression I used in the setup() would just run through that code without pausing and then end up in the loop(). I hadn't considered putting an expression in the loop() part which could run only once.

If I try again and fail I will post the code that I failed with.


wow me to i thought for a sec somethig was wrong with my browser.
We got to see some code to give u much help.
why does your loops/ and configuration have to run in setup.
could create a initialize/config function u call  useing an if statment and is never called until reset in loop. try moveing it around.
or u dont need afunction

if (setRangeAlreadyRun == False){
  //check you max /min values and get it all set here

   setrange.... = true;
}

  //then run your other code
reserve setup for initialize var nameing etc.
i think it works better but not alwasy obviously.
but if what u did doesnt work try something like that

Go Up