Integrating different pieces of code plus a few other questions

I've been going over a few different tutorials that look like they will do what I want and while hesitant to ask, Im confused and hope I can get a bit of help.

First I'll lay out what I want to do and then link the tutorials that I believe should be able to work together.

Im using an Uno with 5 leds on Digital pins 2,3,4,7,8 set as a bar graph style voltmeter

I have a pullup wired mom switch on Digital pin 9

I have a voltage divider variable input on Analog 0

and a transistor output (pulled to ground) on Digital pin 10

I want the project to work like this

POWER ON>>>

READ A0 and display it as a bar graph on D2,3,4,7,8

IF

Pin10 goes LOW

AND

A0 count is between 40 and 180

THEN

Pin10 goes low for 20 minutes

IF

A0 count is above 190 //low level

OR

Pin9 (Button) is pressed for any amount of time beyond debounce time //emergency turn off

THEN

Pin10 will go HIGH and cannot be turned back on until power is cycled.

-During this countdown and after PIN10 has gone HIGH again, the bar graph should continue to function until power is cycled.

I have the LED bar graph working just fine based on this tutorial and the counts I got from the voltage divider via serial

What Im confused on is using the A0 input for multiple functions and how I should write the code.

I have code examples from the BaldEngineer.com site

as well as this http://forum.arduino.cc/index.php?topic=223286.0

and I found a metric fk ton of Google search results about millis countdown timers.

I can make a button turn on a light

I can countdown with MILLIS

but what I dont know how to do and am getting hung up on is how the How to do Multiple Things Code example.

What I dont know is if I can just write little bits of code and just void them with names for each little macro that I can call up and if so how should I address them in the code I want to do.

Im looking to learn, not to have the code handed to me, but Im just getting flat out confused with how some of this works. Im coming from some experience with PICAXE and that worked completely different than Arduino so Im trying to relearn. With that code, I would just do Main>GoTo>Return for things, so Im trying to relearn.

mattyb0:
What I dont know is if I can just write little bits of code and just void them with names for each little macro that I can call up and if so how should I address them in the code I want to do.

If I translate that into the regular jargon it sounds like you are thinking of having the different pieces of code in separate functions. That is an excellent idea. It is illustrated in Planning and Implementing a Program. A very important part of the concept is to treat the project as a series of separate parts rather than thinking of it as complex whole.

I assume this "am getting hung up on is how the How to do Multiple Things Code example" refers to the demo Several Things at a Time. If you post the code you are having problems with and explain what it does, and what you want it to do I will try to help.

...R

a transistor output (pulled to ground) on Digital pin 10

What causes pin 10 to go HIGH / LOW?

Robin2:
If I translate that into the regular jargon it sounds like you are thinking of having the different pieces of code in separate functions. That is an excellent idea. It is illustrated in Planning and Implementing a Program. A very important part of the concept is to treat the project as a series of separate parts rather than thinking of it as complex whole.

I assume this “am getting hung up on is how the How to do Multiple Things Code example” refers to the demo Several Things at a Time. If you post the code you are having problems with and explain what it does, and what you want it to do I will try to help.

…R

Yep, you got the general idea of what Im talking about

As of right now this is what I have

const int FuelSender = A0; // the pin that the potentiometer is attached to
const int ledCount = 5; // the number of LEDs in the bar graph
int ledPins[] = {2, 3, 4, 7, 8}; // an array of pin numbers to which LEDs are attached
int led = 9;    
int button = 10;

int ledState = LOW;            
long previousMillis = 0;        

long interval = 1000;          
void setup() {
   // loop over the pin array and set them all to output:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      pinMode(ledPins[thisLed], OUTPUT);
      pinMode(led, OUTPUT);
      pinMode(button, INPUT);  
   }
}

void loop() {
   // read the potentiometer:
   int sensorReading = analogRead(FuelSender);
   // map the result to a range from 0 to the number of LEDs:
   int ledLevel = map(sensorReading, 40, 200, 0, ledCount);
   // loop over the LED array:
   for (int thisLed = 0; thisLed < ledCount; thisLed++) {
      // if the array element's index is less than ledLevel,
      // turn the pin for this element on:
      if (thisLed < ledLevel) {
         digitalWrite(ledPins[thisLed], HIGH);
      }else { // turn off all pins higher than the ledLevel:
         digitalWrite(ledPins[thisLed], LOW);
      }
if(digitalRead(button) == LOW) {
 unsigned long currentMillis = millis();
  if(currentMillis - previousMillis > interval) {
     previousMillis = currentMillis;  

   if (ledState == LOW)
   ledState = HIGH;

   else
     ledState = LOW;
   digitalWrite(led, ledState);
 
   
 }
 }
 
   }
}

As it works right now, the bar graph works dead on, no issues there. When I press the button, pin 9 turns on, but it doesn’t do a time out.

I messed with different things including the interval but it just changes the way it turns on or off when I press the button (seems to be different times adjust when the light comes on after I initial press the button).

I can work through this with some help as I go, so I hope you can bear with me.

Right now, Id like to make the led on pin9 turn on and off with the button press as well as figure out how to make it turn on after a press and stay on for a set time unless I press the button again prior to it timing out. Once I figure that out, I want to add the additional code that if the analog input on A0 is hits a certain count, it will also turn off pin9.

I tried to map out my goals in my OP so if you can help me get there saying I was happy would be an understatement.

Check what you have in the for loop. It seems that it should only drive the LED display based on the input voltage but you also read the input on the button pin for each iteration of the for loop. The amount of code in the for loop becomes more apparent if you Auto Format your code in the IDE

A suggestion would be to put the reading of the voltage, the for loop and the voltage tests and LED setting that it does into a function and call that from loop()