Go Down

Topic: Embedded Loop (Read 244 times) previous topic - next topic

brhyaa729

Hi guys, I have my CO sensor hooked up and managed to write a PWM duty cycle for 1.4 and 5 volts. Right now I am getting an analog readout of the CO level every 2 1/2 minutes. Is there a way to embed a loop in the my code to give analog pin 0 a readout every 1 seconds during both duty cycles?
Here is my code...

int coPin = 9;    // CO Sensor connected to digital pin 9
void setup() {
  Serial.begin(9600);
}

void loop()  {
  // Define 5 Volt Value
  int HighValue = 255 ; 
    // Sets the 5 Volt Value
    analogWrite(coPin, HighValue);         
    int sensorValue = analogRead(A0);
    Serial.println(sensorValue);
    delay(60000UL);                           
   

  // Define 1.4 Volt Value
  int LowValue = 73 ;
    // Sets the 1.4 Volt Value
   analogWrite(coPin, LowValue);       
    delay(90000UL);   
   
  }



Thank you !!!





Grumpy_Mike

Yes stop using delays, or use them more intellegently.
Not too sure what you want to do but just set the analogue level and then read the input as many times as you want. Look up how to use for loops.

holmes4

Look at the blink without delay example.

Mark

PeterH

The most general approach is to get rid of the delays using the techniques demonstrated in the 'blink without delay' example.

For something this simple, there is a quick hack which will achieve what you want. Replace this:

Code: [Select]

int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(60000UL);      


With this:

Code: [Select]

for(int i = 0; i < 60; i++)
{
   int sensorValue = analogRead(A0);
   Serial.println(sensorValue);
   delay(1000UL);      
}

(Similarly for the other delay() call.)

Just bear in mind that this approach does not extend at all well, and in general if you want to do more than one thing at once you really need to get rid of the delays.
I only provide help via the forum - please do not contact me for private consultancy.

Go Up