Pages: [1]   Go Down
Author Topic: Activating audio from bent circuit  (Read 543 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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. 

Code:
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);
  }
}







Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8064
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A3 is not a PWM output so you can't pulse-width-modulate it with analogWrite().


    analogWrite(siren,50); will act as digitalWrite(siren, LOW); because 50 < 128

    analogWrite(siren,200); will act as digitalWrite(siren, HIGH); because 20 >= 128

    analogWrite(siren,LOW); will act as digitalWrite(siren, LOW); because 0 < 128


Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.   



Code:
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);
  }   
  }










Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
To stop immediately, is an interrupt necessary?
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.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This attempt is not responding to sensor input.  The audio begins immediately without end.

Code:
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 sonarPin = A0;
int sonarVal =0;

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);
  pinMode(sonarPin,INPUT);
}
void loop()
{
  static int state1 = 1;
  static int state2 = 1;
  sonarVal = analogRead (sonarPin);
  Serial.println (sonarVal);
  delay (50);
  switch (state1)
  { 
  case 1:
    if (sonarVal >5)
    {
      digitalWrite(siren,LOW);
      digitalWrite(snd1,LOW);
      digitalWrite(snd2,LOW);
      digitalWrite(snd3,LOW);
      digitalWrite(snd4,LOW);
      digitalWrite(snd5,LOW);
    }
    else
    {
      state1 = (2,3,4);   

    }
    break;
  case 2:
    if (sonarVal < 15)
    {
      randOn = random (50, 5000);
      randOff = random (1, 900);
      digitalWrite(siren,HIGH);
      delay(randOn);

      digitalWrite(siren,LOW);
      delay(randOff);

    }
    else
    {
      state1 = 1;         
    }
    break;
  case 3:

    if (sonarVal < 15)
    {
      digitalWrite(snd1,HIGH);
      delay(5000);
      digitalWrite(snd1,LOW);
    }
    else
    {
      state1 = 1;
      {       

        if (sonarVal < 15)
        {
          break;
        case 4:
          digitalWrite(snd2,HIGH);
          delay(5000);
          digitalWrite(snd2,LOW);
        }
        else
        {
          state1 = 1;
        }
        break;
      }

    }

  }
}















Logged

Poole, Dorset, UK
Offline Offline
Edison Member
*
Karma: 25
Posts: 1872
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!.

Mark
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Code:
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;
      {
      }
    }
  }
}




Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
      state1 = (2,3,4);   
What, exactly, do you think this is doing? It most certainly isn't.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 548
Posts: 46026
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Unfortunately, she didn't appreciate my embrace
So, you rejected her:
Code:
  delay(100);       
  delay(500);
      delay(1000);
      delay(1000);

And, what the hell is this for:
Code:
      {
      }
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 146
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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).
Logged

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 508
Posts: 31382
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
"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.

Quote
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.

Logged

Pages: [1]   Go Up
Jump to: