Hi everyone, thanks for reading. (my english may not be the best, I'm sorry about that).
The project I'm working on consist in turning on 3 LED, one after every second a person is blowing through a flexible pipe.
I'm using a IR emitter and receiver for checking the movement of an helix that spins with the air blown through the tube. Everytime I get a difference in the IR signal (from continuous to interrupted and viceversa) I count a 'revolution'. Once one revolution is detected, I check if one second passed, once it has, I check if there are a minimum of revolutions (so I can assure there's been a constant blowing through the pipe) and if there are, I turn on the first LED.
I want to count three seconds continuously and turn one LED every second of constant blowing.
My problem is that after the first LED is lit, I want to check the next second and if I still get the minimum of revolutions within that second, turn the second LED on. The same with the third led in the third second.
I get the first LED turned on after 1000millis and at least 10 revolutions, but the other 2 turn on really quick and there is no 1 second waiting time.
I'm not very good at programming so I feel I may be using silly code in some parts, if you have any suggestions on how I could optimize some parts of the code It would be great.
Thank you in advance,
Natalia.
My code, i deleted the commented lines for debugging so its quickier to read.
const int sensorPin=A0;
const int ledApin=4; // if I put it in 1, it would be turned on all the time even if i set the LED to LOW.
const int ledBpin=2;
const int ledCpin=3;
int irCommStatus; // 0 for continuous IR beam (sensor values under 100). 1 for interrupted IRbeam (100+).
int prevIrCommStatus; // stores the previous value
int revolutions = 0; // counts how many changes in irCommStatus have been.
int currentLED = 0; //id for showing which led is turned on
long previousMillis =0; // store the last time this was used
long interval = 1000; // is 1 second, but this sketch has a delay of 2millis. so idk if I should change it.
void setup()
{
pinMode(sensorPin,INPUT);
pinMode(ledApin,OUTPUT);
pinMode(ledBpin,OUTPUT);
pinMode(ledCpin,OUTPUT);
Serial.begin(9600);
}
void loop(){
int sensorVal=analogRead(sensorPin);
prevIrCommStatus = irCommStatus; // before refreshing the var val, store the previous value.
if (sensorVal< 70){
irCommStatus = 0;
} else if(sensorVal > 100){
irCommStatus = 1;
}
if (prevIrCommStatus != irCommStatus){
revolutions++; // we asumme the helix spinned and we add 1 revolution.
} // everything works perfect until here.
if (revolutions > 1){ //if there is at least one revolution, I start to count the time.
unsigned long currentMillis = millis(); //taken from blink without delay example
if (currentMillis - previousMillis > interval) { //check if the last time was longer ago than the interval
if (revolutions > 10){ // if there is at least 10 revolutions
switch(currentLED){ // I turn the LED on according to the currentLED var
case 0:
digitalWrite(ledApin, HIGH);
revolutions =1; // I do not set it to 0 because I have to check the three seconds continously
break; // and It would have to wait to another revolution to start counting again
case 1:
digitalWrite(ledBpin,HIGH);
revolutions =1;
break;
case 2:
digitalWrite(ledCpin,HIGH);
revolutions =1;
break;
case 3:
currentLED =0; // this turns off my 3 leds, not sure why. I just wanted to reset the counter
// because It'd go to infinite.
}
previousMillis = currentMillis; //set last time as the current time
// currentLED++; // this was what I was trying to set the LEDcounter one forward
} else if (revolutions < 10) { //if there are less than 10 revolutions per second in any of the seconds
digitalWrite(ledApin, LOW); // turn off all the LEDs
digitalWrite(ledBpin, LOW);
digitalWrite(ledCpin, LOW);
revolutions=0;
previousMillis = currentMillis;
}
}
}
delay(2); // this delay works better than 1 or higher for reading my sensors.
}
THANKS!!!!!