Good day ! I have a selector switch ( interlock ) with which I want to select the on-time of a LED which turns on when you press the start button and it turns off when the time is over or when i put the selector in off position. Where the first switch position is OFF and the following four positions are different times.
The problem is that when I select OFF should sound a warning buzzer for 1 second (to indicate that the time was interrupted ) likewise should sound for 1 second when the time is over (to indicate that the cycle ended ). But I tried to do with the Function "first time" but the led only turns on just it once , I tried with a delay but it repeats while the selector is in OFF. I implemented my code with switch statement function as follows :
const int min = 0;
const int max = 1023;
const int led=13;
const int start=10;
int ledState = LOW;
long OnTime = 5000;
unsigned long previousMillis = 0;
int buzzer=3;
void setup() {
pinMode (buzzer, OUTPUT);
pinMode (led, OUTPUT);
Serial.begin(9600); }
void loop() {
int sensorReading = analogRead(A0);
int range = map(sensorReading, min, max, 0, 7);
unsigned long currentMillis = millis();
switch (range) {
case 0: //Potentiometer turned between 0-25%
ledState=LOW;
//Here the led should turns on just once and only for a second
Serial.println("OFF");
break;
case 1: //Potentiometer turned upto 26-50%
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime)) {
ledState = LOW; // Turn it off
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 1");
break;
case 2: //Potentiometer turned upto 51-75%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 2000))) {
ledState = LOW; // Turn it off
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 2");
break;
case 3: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 4000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 3");
break;
case 4: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
case 5: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
case 6: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
case 7: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
}
digitalWrite(led, ledState);
delay(100);
}
I appreciate your help!
Maybe something along these lines:
int buzzer_on_time = 0;
void handle_buzzer()
{
if( buzzer_on_time > 0 )
{
// Sound buzzer here ...
}
buzzer_on_time -= millis();
}
Call handle_buzzer() at the bottom of your loop(), then whenever you want to sound the buzzer, just set buzzer_on_time to however long you want it on for.
It sounds to me like you want two possible actions to happen when the case is 0. Either the process starts (which you already have) or the process terminates - which needs extra code to test the state of the process - whether it is already running or is currently stopped.
...R
Tammytam i tried something like this, but it runs just once. I need buzzer sounds 1 second each led turns off. But i think i did it wrong, i have some questions i used correctly the void handle_buzzer()? And the if condition is inside my case 0? I´m a begginer in this.
Thank´s!
const int min = 0;
const int max = 1023;
const int led=13;
const int start=10;
int ledState = LOW;
int buzzerState=LOW;
long OnTime = 5000;
unsigned long previousMillis = 0;
int buzzer=3;
int buzzer_on_time = 0; //NEW LINE
void setup() {
void handle_buzzer(); //NEW LINE
pinMode (buzzer, OUTPUT);
pinMode (led, OUTPUT);
Serial.begin(9600);
}
void loop() {
int sensorReading = analogRead(A0);
int range = map(sensorReading, min, max, 0, 7);
unsigned long currentMillis = millis();
switch (range) {
case 0:
ledState=LOW; //Here the led should turns on just once and only for a second
Serial.println("OFF");
break;
No thats not quite right. Also, it won't jsut work on its own without detecting a change in range. This is what your after. (Havent checked the code, I've just placed the new parts in the correct places:
const int min = 0;
const int max = 1023;
const int led=13;
const int start=10;
int ledState = LOW;
long OnTime = 5000;
unsigned long previousMillis = 0;
int buzzer=3;
int buzzer_on_time = 0;
int last_range = -1;
void handle_buzzer()
{
if( buzzer_on_time > 0 )
{
// Sound buzzer here ...
}
buzzer_on_time -= millis();
}
void setup()
{
pinMode (buzzer, OUTPUT);
pinMode (led, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int sensorReading = analogRead(A0);
int range = map(sensorReading, min, max, 0, 7);
unsigned long currentMillis = millis();
switch (range)
{
case 0: //Potentiometer turned between 0-25%
ledState=LOW;
//Here the led should turns on just once and only for a second
Serial.println("OFF");
if( last_range != range )
buzzer_on_time = 1000;
break;
case 1: //Potentiometer turned upto 26-50%
if((ledState == HIGH) && (currentMillis - previousMillis >= OnTime))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 1");
break;
case 2: //Potentiometer turned upto 51-75%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 2000)))
{
ledState = LOW; // Turn it off
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 2");
break;
case 3: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 4000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 3");
break;
case 4: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
case 5: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
case 6: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
case 7: //Potentiometer turned upto 76-100%
if((ledState == HIGH) && (currentMillis - previousMillis >= (OnTime + 6000)))
{
ledState = LOW;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
else if ((ledState == LOW) && digitalRead(start)== HIGH)
{
ledState = HIGH;
previousMillis = currentMillis;
digitalWrite(led, ledState);
}
Serial.println("TIEMPO 4");
break;
}
last_range = range;
digitalWrite(led, ledState);
delay(100);
}
Thank's Tammytam i see it good! I'll try the code at a time. I notice you the results.
Robin2, Its ok Robin i want two conditions:
Condition 1: I have diferents time on for a led in each case. I turn on led with a start boton and time on starts, when time is over the led is turned down and here should sound my buzzer for 1 minute and then it turn down just once. But obviously it needs be repeated every my led output is turned on.
Condition 2: I have the case 0, where the led time on can be interrupted and the led output is turned down when this case is true. Also i need my buzzer turn on for a second and then it should down (it to notice that led was turned down). It needs repeat each i starts the led on.
Do you have any option? Thanks!
ArmandoAmaros:
Robin2, Its ok Robin i want two conditions:
Perhaps I am misunderstanding things - what is not clear to me is what triggers the conditions.
I was guessing from your code that when the potentiometer is at Zero everything is off and case 0 is triggered. Then you turn it and case 1 is triggered (or case 2 etc). And when it goes back to Zero (case 0) it should make some noises and switch off.
From your Original Post
case 0: //Potentiometer turned between 0-25%
ledState=LOW;
//Here the led should turns on just once and only for a second
When I wrote my previous paragraph it occurred to me that you are using case0 to start things and there needs to be a case before that to represent the Zero position when things should be switched off.
I hope that makes sense...
Let me set out the way I would do the steps
Step0 - pot at Zero position - all off.
if Step0 is reached while the system is working make a noise and switch off / reset counter / whatever
Step1 - pot at first position
start things happening
Step3 - pot at second position
do more things
...R
Excuse me Robin2 and Tammytan, i´ve been working in my project and i found a way to make my objetive. This code is probably very extensive but it´s working good, only not sound my buzzer when i move the selector to my off position. I´m working in this and i have the idea, as you tell me with a previous state comparision, i´m going to try it and if you have an idea to optimize my code is wellcome.
Thank´s!!
const int min = 0;
const int max = 1023;
const int reac=7;
const int start=4;
int reacState = LOW;
long OnTime = 120000;
unsigned long previousMillis = 0;
int buzzer=9;
int lamp=12;
int vent=3;
void setup() {
pinMode (buzzer, OUTPUT);
pinMode (reac, OUTPUT);
pinMode (lamp, OUTPUT);
pinMode (vent, OUTPUT);
Serial.begin(9600);
}
void loop()
{
if(reacState== HIGH) {
digitalWrite(vent, HIGH); }
else {
digitalWrite(vent, LOW); }
digitalWrite(lamp, HIGH);
int sensorReading = analogRead(A0);
int range = map(sensorReading, min, max, 0, 7);
unsigned long currentMillis = millis();
switch (range) {
case 0:
reacState=LOW;
Serial.println("OFF");
if(digitalRead(start) == HIGH)
{ for (int x = 0; x < 3; x++) {
digitalWrite(buzzer, HIGH);
digitalWrite(vent, HIGH);
delay(100);
digitalWrite(buzzer, LOW);
digitalWrite(vent, LOW);
delay(100); }}
break;
case 1:
if((reacState == HIGH) && (currentMillis - previousMillis >= OnTime)) {
for (int x = 0; x < 1; x++) {
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
}
reacState = LOW;
previousMillis = currentMillis;
digitalWrite(reac, reacState);
}
else if ((reacState == LOW) && digitalRead(start)== HIGH)
{
reacState = HIGH;
previousMillis = currentMillis;
digitalWrite(reac, reacState);
}
Serial.println("TIEMPO 1");
break;
case 2:
if((reacState == HIGH) && (currentMillis - previousMillis >= (OnTime + 180000)))
{
for (int x = 0; x < 1; x++) {
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
}
reacState = LOW;
previousMillis = currentMillis;
digitalWrite(reac, reacState);
}
else if ((reacState == LOW) && digitalRead(start)== HIGH)
{
reacState = HIGH;
previousMillis = currentMillis;
digitalWrite(reac, reacState);
}
Serial.println("TIEMPO 2");
break;
....etc.