This sketch has been edited a dozen different ways with the same result; sounds 1~5 fire as they should but the random buzzer emits only a single beep. The sounds are salvaged from children't toys triggered by a photocell. In a separate sketch, the buzzer is functional but not here. I assume this is a software error. Appreciate any suggestions. "randOn" begins code for the buzzer.
int drummer = 12;
int siren = A3;
long randOn =0;
long randOff =0;
int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;
int sensorPin = A0;
int sensorValue = 0;
void setup(){
pinMode(drummer, OUTPUT);
pinMode(siren, OUTPUT);
randomSeed (analogRead (0));
pinMode(snd1, OUTPUT);
pinMode(snd2, OUTPUT);
pinMode(snd3, OUTPUT);
pinMode(snd4, OUTPUT);
pinMode(snd5, OUTPUT);
digitalWrite(snd1,LOW);
digitalWrite(snd2,LOW);
digitalWrite(snd3,LOW);
digitalWrite(snd4,LOW);
digitalWrite(snd5,LOW);
Serial.begin(9600);
delay(500);
}
void loop()
{
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(500);
if (sensorValue > 15)
{
digitalWrite(snd1,HIGH);
delay(5000);
digitalWrite(snd1,LOW);
//delay(3000);
digitalWrite(snd2,HIGH);
delay(5000);
digitalWrite(snd2,LOW);
// delay(3000);
digitalWrite(snd3,HIGH);
delay(5000);
digitalWrite(snd3,LOW);
digitalWrite(snd4,HIGH);
delay(5000);
digitalWrite(snd4,LOW);
digitalWrite(snd5,HIGH);
delay(5000);
digitalWrite(snd5,LOW);
delay(1000);
randOn = random (50, 500); ----------buzzer
randOff = random (1, 900);
analogWrite(siren,50);
delay(randOn);
analogWrite(siren,200);
delay(randOn);
analogWrite (siren, LOW);
delay(randOff);
}
}
Thanks. The sketch is working. Well, not exactly as it should. Firstly, there is a long, ten second delay after running through the sounds before starting once again. And, when the sensor goes below the threshold, the sketch continues to the end before stopping. To stop immediately, is an interrupt necessary? I have no experience with interrupts but will dive into it if advised this is the solution.
int siren = 3;
long randOn =0;
long randOff =0;
int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;
int sensorPin = A0;
int sensorValue = 0;
void setup(){
pinMode(drummer, OUTPUT);
pinMode(siren, OUTPUT);
randomSeed (analogRead (0));
pinMode(snd1, OUTPUT);
pinMode(snd2, OUTPUT);
pinMode(snd3, OUTPUT);
pinMode(snd4, OUTPUT);
pinMode(snd5, OUTPUT);
digitalWrite(snd1,LOW);
digitalWrite(snd2,LOW);
digitalWrite(snd3,LOW);
digitalWrite(snd4,LOW);
digitalWrite(snd5,LOW);
Serial.begin(9600);
delay(500);
}
void loop()
{
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(500);
if (sensorValue > 15)
{
randOn = random (50, 5000);
randOff = random (1, 900);
digitalWrite(siren,HIGH);
delay(randOn);
digitalWrite(siren,LOW);
delay(randOff);
digitalWrite(snd1,HIGH);
delay(5000);
digitalWrite(snd1,LOW);
//delay(3000);
digitalWrite(snd2,HIGH);
delay(5000);
digitalWrite(snd2,LOW);
// delay(3000);
digitalWrite(siren,HIGH);
delay(randOn);
digitalWrite(siren,LOW);
delay(randOff);
digitalWrite(snd3,HIGH);
delay(5000);
digitalWrite(snd3,LOW);
digitalWrite(snd4,HIGH);
delay(5000);
digitalWrite(snd4,LOW);
digitalWrite(snd5,HIGH);
delay(5000);
digitalWrite(snd5,LOW);
//delay(1000);
}
}
No. You need to read, understand, and embrace the blink without delay example. Then, delete that sketch and start over. Looking into state machines would be good, too.
Read PaulS comment above, but not only do you need to read and embrace "blink without delay" you need to marry it and have kids, then when your an grand pa ask again!.
Unfortunately, she didn't appreciate my embrace, but threw me at once to the dogs! Being ugly, old, and lame, I fear she will need considerable persuasion. A mere hint from your majesty would be most kind.
int sensorPin = A0;
int sensorValue = 0;
int siren = 3;
long randOn =0;
long randOff =0;
int snd1 = 5;
int snd2 = 6;
int snd3 = 7;
int snd4 = 8;
int snd5 = 9;
int sirenState = LOW;
int snd1State = LOW;
int snd2State = LOW;
int snd3State = LOW;
int snd4State = LOW;
int snd5State = LOW;
// ledState used to set the LED
long previousMillis = 0; // will store last time LED was updated
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(siren, OUTPUT);
randomSeed (analogRead (0));
pinMode(snd1, OUTPUT);
pinMode(snd2, OUTPUT);
pinMode(snd3, OUTPUT);
pinMode(snd4, OUTPUT);
pinMode(snd5, OUTPUT);
digitalWrite(snd1,LOW);
digitalWrite(snd2,LOW);
digitalWrite(snd3,LOW);
digitalWrite(snd4,LOW);
digitalWrite(snd5,LOW);
Serial.begin(9600);
delay(100);
}
void loop()
{
sensorValue = analogRead(sensorPin);
Serial.println(sensorValue);
delay(500);
unsigned long currentMillis = millis();
if (sensorValue > 15) {
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
if (sirenState == LOW)
sirenState = HIGH;
delay(1000);
}
else
{
sirenState = LOW;
digitalWrite(siren, sirenState);
if (snd1State == LOW){
snd1State = HIGH;
delay(1000);
}
else
snd1State = LOW;
{
}
}
}
}
With considerably more experience, I may be able to decipher your clues but am presently at a loss where to take this. "state1 = (2,3,4);" was intended to direct the sketch to these "case" when the threshold was surpassed. The "delay" are there to permit the audio that much time to play. As for the brackets { }, I am not sure how to respond. I am not asking anybody to write the sketch for me, but a few specific directions would certainly help. The "blink without delay" example is especially baffling: How can the sensor be included along with all the sounds? A web search for examples doesn't yield similar problems (or, I don't grasp their meaning).
"state1 = (2,3,4);" was intended to direct the sketch to these "case" when the threshold was surpassed.
The trick is using the syntax that the compiler understands. That line means nothing to a C compiler, look up the switch statement in the arduino reference.
As for the brackets { }, I am not sure how to respond.
Braces like this enclose a section of code that you want to treat as just one line. Typically it is used after an if statement to allow more than one line to be conditional on the result of the if. By themselves they mean nothing.
You should only have delays where you absolutely need them, your code is scattered with them most of which are not necessary.
Try and learn to read code and imagine what will happen when each line is executed. Try and think like a processor, one step at a time.