I'm trying to make a simple system which is simple on paper but won't work with the code. I'm a noob just so you know.
The situation is like this.
I have 2 switches
One of them is ON/OFF and the other is a push button.
1 led RED
1 led GREEN
1 led BLUE
The ideea is this.
Button 1 (on/off)
When it's off I want it to read the pushbutton.
When the pushbutton is pushed i want it to turn on the RED led, wait 60 seconds then turn the RED led off and turn on the GREEN one. The pushbutton will be pushed the whole time and i want the GREEN led to stay on until the pushbutton is released.
so that's it when it's on OFF mode:
All leds OFF
Push button is pushed
Red led is ON
count 60 seconds
RED led off and GREEN led is ON.
When the push button is released ALL leds should be OFF and can repeat the sequence if i push the button again.
ON/OFF switch is ON.
When the switch is in ON position i want it to read the push button. ALL LEDS ARE OFF. If that pushbutton is pushed i want it to turn on the BLUE led, wait 60 seconds, then turn on the red led and trigger 5 volts on a pin for 1 second. After the pushbutton is released ALL LEDS ARE OFF and i can repeat the sequence.
so, in ON mode it should be like this:
All Leds off
Push button is pushed and kept that way
BLUE LED is ON
WAIT 60 seconds
BLUE LED IS OFF
RED LED IS ON
Give me 5 volts on one pin.
Can this be done only with an arduino UNO and no other hardware timers and so on?
I've only declared the pins and tryed to use the delay function... but obviously i ran into some problems. The on/off push button delay acts both ways. On pressing and releasing...
And sometimes it won't turn off the other led.
I've read something about milis but... as I sayed I am a noob.
I have this arduino for about 3 days... so you can't expect much from me. but I want to do this project this week-end if it's possible... i hope
This is the chunk that deals with using millis() as a timer and when a time is reached,
an action is performed.
Attempt at copying the example code from that page and modifying it to suit your needs. If need be copying it multiple times as your program grows.
// check to see if it's time to blink the LED; that is, if the
// difference between the current time and last time you blinked
// the LED is bigger than the interval at which you want to
// blink the LED.
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
In this case
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
is used to do the check and update at a set interval. You would likely want to run your timers once.
and
// if the LED is off turn it on and vice-versa:
if (ledState == LOW) {
ledState = HIGH;
} else {
ledState = LOW;
}
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
is the action. Replaced with whatever action you might want.
Give your attempt at integrating your pseudo code into actual code and if you cannot get it to compile or run as you like, help can be given. At this time you would post your entire code.
I'll try it. I'll post my code but you will only see how retarded I am and nothing more. I'm still in the phase of writing without the on/off switch and I'm failing.
const int buttonPin = 2;
const int ledPin = 13;
const int led2Pin = 12;
int buttonState = 0;
//int ledState = LOW;//junk no need for this
//int led2State = LOW;//junk
unsigned long interval = 200; //200ms
unsigned long previousMillis = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(buttonPin, INPUT);//look at INPUT_PULLUP
}
void loop() {
unsigned long currentMillis = millis();//millis is the internal timer
buttonState = digitalRead(buttonPin);
// ledState = digitalRead(ledPin);//junk its a output it will stay where its set
// led2State = digitalRead(led2Pin);//junk
if (buttonState == HIGH) {//hope you have a pull resistor
//if not use INPUT_PULLUP and check for LOW
// led2State = LOW;//junk who cares what state the pin is in
// ledState = HIGH;//junk
//rest of the code looks fine
digitalWrite(ledPin, HIGH);
if (currentMillis - previousMillis >= interval) {
//this will happen 200ms after button is pushed
digitalWrite(led2Pin, HIGH);
}
}
else {
digitalWrite(led2Pin, LOW);
digitalWrite(ledPin, LOW);
previousMillis = currentMillis; //if button not pressed keep updating time stamp
}
}
The red led (first) it's not shutting down completely. It only dimms about 70%. Is that a circuit problem or something in the code? I'm working on a breadbord.
Here is the code now.
const int buttonPin = 2;
const int ledPin = 13;
const int led2Pin = 12;
int buttonState = 0;
unsigned long interval = 200; //200ms
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
pinMode(led2Pin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int sensorVal = digitalRead(2);
Serial.println(sensorVal);
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
if (sensorVal == HIGH) {
//rest of the code looks fine
digitalWrite(ledPin, HIGH);
if (currentMillis - previousMillis >= interval) {
//this will happen 200ms after button is pushed
digitalWrite(led2Pin, HIGH);
digitalWrite(ledPin, LOW);
}
}
else {
digitalWrite(led2Pin, LOW);
digitalWrite(ledPin, LOW);
previousMillis = currentMillis; //if button not pressed keep updating time stamp
}
}
If I don't use digitalwrite after the ELSE the green led will be ON at the release of the button.
I have changed the code names but i taught it is easyer without the long names at the beginning.
I have also added a buzzer when the red led turns off but it beeps continuously. I'm searching for a function to stop it after one beep. I'm not posting the code yet... i'll post it when i'm giving up
It's not ready. The picture above was from the button example.
So... regarding the buzzer... I simply give up. I cleaned all my attempts to end the buzzer at one beep but i failed. Also searched on google and nobody wats to stop the beep after one count...
What do you guys reccomend?
const int redPin = 13;
const int greenPin = 12;
const int buzzerPin = 11;
const int bluePin = 10;
const int switchPin = 9;
const int buttonPin = 2;
int buttonState = 0;
int switchState = 0;
unsigned long interval = 2000; //200ms 60000/ 1min
unsigned long previousMillis = 0;
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
int sensorVal = digitalRead(2);
Serial.println(sensorVal);
unsigned long currentMillis = millis();
buttonState = digitalRead(buttonPin);
switchState = digitalRead(switchPin);
if (sensorVal == HIGH) {
//rest of the code looks fine
digitalWrite(redPin, HIGH);
if (currentMillis - previousMillis >= interval) {
//this will happen 200ms after button is pushed
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
for (int i=0; i<300; i++) { // generate a 1KHz tone for 1/2 second
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(100);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(300);
digitalWrite(bluePin, HIGH);
}
}
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
previousMillis = currentMillis;
}
}
please stop digitally reading the same button 2 different ways and if you change code on a example change the text to show you know what affect the change is making
example code untested but its a correction of your code
const int redPin = 13;
const int greenPin = 12;
const int buzzerPin = 11;
const int bluePin = 10;
const int switchPin = 9;
const int buttonPin = 2;
byte buzzerRequest = 0;
byte onetime = 0;
unsigned long interval = 2000; //2 seconds
unsigned long interval2 = 500;
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
}
void loop() {
unsigned long currentMillis = millis();
byte button = digitalRead(buttonPin);//push button
byte m_switch = digitalRead(switchPin);//switch maintained
if (button == LOW) { //button is pressed as this is a input pullup
digitalWrite(redPin, HIGH);
if (currentMillis - previousMillis >= interval) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
if (onetime == 0) {//only do this once unless button is released
buzzerRequest = 1;//tell buzzer we require noise
previousMillis2 = currentMillis;//set the time stamp to turn buzzer off
onetime = 1;//block this if so it only goes onetime
}
}
}
else {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
previousMillis = currentMillis;
buzzerRequest = 0;
onetime = 0;
}
if (currentMillis - previousMillis2 >= interval2) {
buzzerRequest = 0;
}
if (buzzerRequest == 1) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(100);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(300);
digitalWrite(bluePin, HIGH);
}
}
The buzzer count from your code works. However my LED's go nuts.
I'll post the final code when I'm done. I've added the second switch and second set of commands.
Without the acoustic and the trigger everything works as I intended. I have also made notes in the code and added a 3'rd time interval (for the trigger).
const int redPin = 13;
const int greenPin = 12;
const int bluePin = 11;
const int buzzerPin = 10;
const int switchPin = 9;
const int triggerPin = 8;
const int buttonPin = 2;
byte buzzerRequest = 0;
byte onetime = 0;
int buttonState = 0;
int switchState = 0;
unsigned long interval = 2000; //200ms 60000/ 1min
unsigned long interval2 = 800; // Used for BUZZER
unsigned long interval3 = 100; // Used for TRIGGER
unsigned long previousMillis = 0;
unsigned long previousMillis2 = 0;
unsigned long previousMillis3 = 0;
//_________________________________________________OK
void setup() {
Serial.begin(9600);
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(triggerPin, OUTPUT);
pinMode(switchPin, INPUT_PULLUP);
pinMode(buttonPin, INPUT_PULLUP);
}
//_________________________________________________OK
void loop() {
int sensorVal = digitalRead(2);
int switchVal = digitalRead(9);
Serial.println(sensorVal);
Serial.println(switchVal);
unsigned long currentMillis = millis();
unsigned long currentMillis2 = millis();
unsigned long currentMillis3 = millis();
buttonState = digitalRead(buttonPin);
switchState = digitalRead(switchPin);
//_________________________________________________OK
// Switch ON
if (switchVal == HIGH) {
if (sensorVal == HIGH) {
digitalWrite(bluePin, HIGH);
if (currentMillis - previousMillis >= interval) {
digitalWrite(redPin, HIGH);
digitalWrite(bluePin, LOW);
for (int i=0; i<300; i++) { // BUZZER needs to beep only once
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(100);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(300);
digitalWrite(triggerPin, HIGH); // trigger must be 1 second long - ONLY
}
}
}
else {
// System shutdown when button is released! NEEDED because it works with the hardware. Otherwise LED's go crazy.
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(triggerPin, LOW);
previousMillis = currentMillis;
// System shutdown! NEEDED.
}
}
else
// SWITCH OFF!
if (sensorVal == HIGH) {
digitalWrite(redPin, HIGH);
if (currentMillis - previousMillis >= interval) {
digitalWrite(redPin, LOW);
digitalWrite(greenPin, HIGH);
for (int i=0; i<300; i++) {
digitalWrite(buzzerPin, HIGH);
delayMicroseconds(100);
digitalWrite(buzzerPin, LOW);
delayMicroseconds(300);
}
}
}
else {
// System shutdown when button is released! NEEDED because it works with the hardware. Otherwise LED's go crazy.
digitalWrite(redPin, LOW);
digitalWrite(greenPin, LOW);
digitalWrite(bluePin, LOW);
digitalWrite(triggerPin, LOW);
previousMillis = currentMillis;
// System shutdown! NEEDED.
}
}
Format your code properly (use the AutoFormat tool) so you can easily see which IFs and ELSEs etc belong together.
My suggestion in Reply #10 was based on the possibility that code was in the wrong place.
And please describe what the latest code (in Reply #18) actually does and what it should do. It is too difficult to infer that accurately from the sequence of Posts.