Go Down

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


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() {

void loop()  {
  // Define 5 Volt Value
  int HighValue = 255 ; 
    // Sets the 5 Volt Value
    analogWrite(coPin, HighValue);         
    int sensorValue = analogRead(A0);

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

Thank you !!!


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.


Look at the blink without delay example.



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);

With this:

Code: [Select]

for(int i = 0; i < 60; i++)
   int sensorValue = analogRead(A0);

(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.

Go Up