So , i need when on of the statements are true, both buzzer and red led should start together and if everything is alright to stop after 10 seconds. The problem is they start even without the statements.
The alarm should sound like an alarm that pulses for 10 seconds. Two different sounds tell each which problem is occurring., i.e. the sound for a temperature problem should be different than the sound for a brightness problem.
This is my code:
#include "pitches.h"
int TempC = A1;
const int NTC_IN = A1;
const int TEMP_THRESHOLD = 35;
const int LDR_IN = A2;
int lightsensor = 0;
const int LIGHT_THRESHOLD = 100;
const int Buzzer = 3;
const int Buzzer2 = 11;
const byte LEDPIN1 = 4;
const int LEDPIN2 = 5;
const int LEDPIN3 = 6;
const int LEDPIN4 = 7;
const int BUTTON1 = 8;
const int BUTTON2 = 9;
int ledState = LOW;
int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};
unsigned long previousMillis = 0;
unsigned long toneStartMillis = 0;
const unsigned long blinkInterval = 500;
const unsigned long toneInterval = 10000;
void setup() {
// put your setup code here, to run once:
pinMode(LEDPIN1, OUTPUT);
pinMode(LEDPIN2, OUTPUT);
pinMode(LEDPIN3, OUTPUT);
pinMode(LEDPIN4, OUTPUT);
pinMode(BUTTON1, INPUT);
pinMode(BUTTON2, INPUT);
pinMode(Buzzer, OUTPUT);
digitalWrite(LEDPIN1, ledState);
digitalWrite(LEDPIN2, OUTPUT);
Serial.begin(9600);
}
void loop() {
int temp = analogRead(TempC); //Read the analog pin
temp = temp * 0.48828125; // convert output (mv) to readable celcius
Serial.print("Temperature: ");
Serial.print(temp);
Serial.println("C"); //print the temperature status
delay(1000);
lightsensor = analogRead(LDR_IN);
Serial.print("LightLevel = ");
Serial.print(lightsensor);
delay(1000);
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= blinkInterval) {
previousMillis = currentMillis;
if (ledState == LOW) {
ledState = HIGH;
}else {
ledState = LOW;
}
digitalWrite(LEDPIN1, ledState);
}
int brightness = analogRead(LDR_IN);
int temp = analogRead(NTC_IN);
if (brightness > LIGHT_THRESHOLD ){
digitalWrite(LEDPIN2, LOW);
tone(3,1,NOTE_C8);
toneStartMillis = currentMillis;
}
if (currentMillis - toneStartMillis >= toneInterval) {
noTone(3);
}
}
const int ledPin = 4;// the number of the LED pin
const int buzzerPin = 3;
const int LDR_IN = A2;
const int LIGHT_THRESHOLD = 82;
// Variables will change:
int ledState = LOW; // ledState used to set the LED
// Generally, you should use "unsigned long" for variables that hold time
// The value will quickly become too large for an int to store
unsigned long previousMillis = 0; // will store last time LED was updated
unsigned long toneStartMillis = 0;
// constants won't change:
const unsigned long blinkInterval = 1000; // interval at which to blink (milliseconds)
const unsigned long toneInterval = 10000;
void setup() {
// set the digital pin as output:
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, ledState);
}
void loop() {
// here is where you'd put code that needs to be running all the time.
// 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 >= blinkInterval) {
// 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);
}
int brightness = analogRead(LDR_IN);
if (brightness > LIGHT_THRESHOLD) {
tone(buzzerPin, 440);
toneStartMillis = currentMillis;
}
if ( currentMillis - toneStartMillis >= toneInterval ) {
noTone(buzzerPin);
}
}
I just want to have connection between beeping and blinking together following the statement -> if (brightness > LIGHT_THRESHOLD)
Slowmotion:
Here is the main code without any editing:
I was expecting to see a longer program but this is shorter than the code in your Original Post.
I just want to have connection between beeping and blinking together following the statement -> if (brightness > LIGHT_THRESHOLD)
Sorry, but I am clearly not on the same wavelength as you. Perhaps you can just describe what you want the program to do in plain English without any reference to code.
Robin2:
I was expecting to see a longer program but this is shorter than the code in your Original Post.
Sorry, but I am clearly not on the same wavelength as you. Perhaps you can just describe what you want the program to do in plain English without any reference to code.
...R
Whenever it gets too hot or too bright in the reactor the alarm is triggered and the system is in alarm operation mode.The red LED is blinking and the buzzer produces sound when the alarm is triggered, otherwise they are turned off.The alarm should sound like an alarm that pulses for 10 seconds.
This is what i want to do, but even if it is normal temparature and bright the RED LED blinking non stop, but for the buzzer is working how it should be. I dont know how to explain it more.
If it were me, I would probably do something like:
enum {off, on}; //off is same as 0, on is same as 1
int brightAlarm = off;
void setup(){
//Setup stuff here
}
void loop(){
//loop stuff here
int brightness = analogRead(LDR_IN);
if (brightness > LIGHT_THRESHOLD) {
brightAlarm = on;
} else {
brightAlarm = off;
}
soundBrightAlarm();
}
void soundBrightAlarm(){
if(brightAlarm == on){
//do the timing, etc for the alarm here
}
}
And just add the same for the heat alarm. Of course, without a button, you'll have to listen to the alarm till you get the heat or brightness below your thresholds.
Slowmotion:
Whenever it gets too hot or too bright in the reactor the alarm is triggered and the system is in alarm operation mode.The red LED is blinking and the buzzer produces sound when the alarm is triggered, otherwise they are turned off.The alarm should sound like an alarm that pulses for 10 seconds.
That sounds as if you need code like this pseudo code
temperature = readTempSensor()
brightness = readLightSensor()
if (alarmOn == false) {
if (temperature > allowedTemerature and brightness > allowedBrightness) {
alarmStartMillis = millis()
alarmOn = true;
}
}
if (millis() - alarmStartMIllis < alarmOnInterval) {
soundAlarm()
blinkLED()
}
else {
alarmOn = false
}
The way you have it written is, I'll say, "similar" to what I thought of originally. But after reading #1 again, I noticed the OP wants 2 distinct tones to distinguish audibly what needs attention. That tidbit was, however, omitted from reply #5. So, who knows?