Pages: [1]   Go Down
Author Topic: Embedded Loop  (Read 191 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 26
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !!!




Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 508
Posts: 31416
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look at the blink without delay example.

Mark
Logged

UK
Offline Offline
Shannon Member
****
Karma: 184
Posts: 11162
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
int sensorValue = analogRead(A0);
Serial.println(sensorValue);
delay(60000UL);      

With this:

Code:
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.
Logged

I only provide help via the forum - please do not contact me for private consultancy.

Pages: [1]   Go Up
Jump to: