EEVblog Electronics Community Forum

Electronics => Microcontrollers => Topic started by: david-electrical on May 27, 2016, 09:37:53 am

Title: how to solve the code problem when i made music Fire alarm by arduino?
Post by: david-electrical on May 27, 2016, 09:37:53 am
When i made music fire alarm by arduino,i met a problem,the errow shows: empty character constant

the sensor perception the fire,then ARDUINO control buzzer,when fire closer then music to alarm

int flame=A5;
int Beep=9;
int length=15;
char notes[]="ccggaagffeeddc ";
int beats[]={1,1,1,1,1,1,2,1,1,1,1,1,1,2,4};
int tempo=300;

int val=0;
void playTone(int tone ,int duration)
{
for(long i=0;i<duration*1000L;i+=tone*2) {
digitalWrite(Beep,HIGH);
delayMicroseconds(tone);
digitalWrite(Beep,LOW);
delayMicroseconds(tone);
}
}
void playNote(char note,int duration )
{
char names[]={'c','d','e','f','g','a','b','C'};
int tones[]={1915,1700,1519,1432,1275,1136,1014,956};
for(int i=0;i<8;i++)
{
  if(names==note)
playTone(tones,duration);
}
}
void setup()   
{
  pinMode(Beep,OUTPUT); 
pinMode(flame,INPUT);
Serial.begin(9600);
}   
void loop() {
  val=analogRead(flame); 
Serial.println(val); 
if(val>=600) 
{ digitalWrite(Beep,HIGH);
   for(int i=0;i<length;i++)
   {
  if(notes=='')
delay(beats*tempo);
  else
  playNote(notes,beats*tempo);
  delay(tempo/2);
   }
}
else {
  digitalWrite(Beep,LOW);
     }
}


Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: Mechatrommer on May 27, 2016, 10:01:03 am
1st thing 1st.. make proper indentation...
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: obiwanjacobi on May 27, 2016, 10:13:40 am
Code: [Select]
int flame = A5;
int Beep = 9;
int length = 15;
char notes[] = "ccggaagffeeddc ";
int beats[] = { 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 4 };
int tempo = 300;

int val = 0;
void playTone(int tone, int duration)
{
    for (long i = 0; i<duration * 1000L; i += tone * 2)
    {
        digitalWrite(Beep, HIGH);
        delayMicroseconds(tone);
        digitalWrite(Beep, LOW);
        delayMicroseconds(tone);
    }
}
void playNote(char note, int duration)
{
    char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
    int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014, 956 };
    for (int i = 0; i<8; i++)
    {
        if (names == note)
            playTone(tones, duration);
    }
}
void setup()
{
    pinMode(Beep, OUTPUT);
    pinMode(flame, INPUT);
    Serial.begin(9600);
}
void loop()
{
    val = analogRead(flame);
    Serial.println(val);
    if (val >= 600)
    {
        digitalWrite(Beep, HIGH);
        for (int i = 0; i<length; i++)
        {
            if (notes == '')
                delay(beats*tempo);
            else
                playNote(notes, beats*tempo);
            delay(tempo / 2);
        }
    }
    else
    {
        digitalWrite(Beep, LOW);
    }
}
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: hendorog on May 27, 2016, 10:39:06 am
I think line 44, which is:
Code: [Select]
if (notes == '')

should instead be this:
Code: [Select]
if (notes[i] == '')

Same issue on line 25 as well
Code: [Select]
if (names[i] == note)

Edit: And line 47
Code: [Select]
playNote(notes[i], beats*tempo);

Edit: and there is another one too, but you can find that one :)
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: botcrusher on May 27, 2016, 12:07:58 pm
I just want to point out that in most jurisdictions this is 100%  against fire code
even presuming your code is bullet proof, until you have it actually approved by a fire marshal / your local group residing over fire law, this is a big nope.
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: dannyf on May 27, 2016, 12:44:50 pm
Quote
music fire alarm

That's an interesting innovation we can live without: soothing and comforting music when the fire is raging.
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: botcrusher on May 29, 2016, 04:29:04 am
Interestingly, HK fire code has a section on alarm parameters, of which one condition is that it is of a certain tone so that it remains recognisable. Making it sing a tune violates that, and removes the urgency from the alarm. Who would exit the building to a hood song?
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: jnz on May 29, 2016, 05:09:33 am
Who would exit the building to a hood song?

Im fairly sure you meant GOOD song... But I want to beleive you really meant hood song. In which case I look forward to the dual discussions about STM F0 vs NXP 1100 peripheral sets while making analogies to Kendrick's Maad City and Alright, or Lupe Fiasco's Prisoner 1&2, or even what "hood" means in terms of good or bad and if JCole is "hood" still despite Role Modelz being excellent or if something more oscure like Tech N9ne is "hood"-enough.

Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: Mechatrommer on May 29, 2016, 06:16:22 am
just a suggestion...
https://www.youtube.com/watch?v=sbbYPqgSe-M (https://www.youtube.com/watch?v=sbbYPqgSe-M)
(ps: i'd be surprised if someone come edit this post saying this is a weird and dangerous advice...)

Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: botcrusher on May 29, 2016, 07:09:50 pm
Oh lol. Yes, i meant "good"
But that's hilarious  :-DD
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: timb on May 29, 2016, 07:33:36 pm
just a suggestion...
https://www.youtube.com/watch?v=sbbYPqgSe-M (https://www.youtube.com/watch?v=sbbYPqgSe-M)
(ps: i'd be surprised if someone come edit this post saying this is a weird and dangerous advice...)

But... Don't you want to know why we keep starting fires? It's my desire! (My desire!) Danger! Danger! High Voltage! When we touch, when we kiss! Fire in the disco, fire in the disco! Fire in the...Taco Bell... Fire in the disco, fire in the gates of hell! Danger, danger! High Voltage...

https://youtu.be/2a4gyJsY0mc
Title: Re: how to solve the code problem when i made music Fire alarm by arduino?
Post by: stmdude on May 29, 2016, 07:51:04 pm
The "empty character constant" message comes from code like this:
Code: [Select]
a = '';
Note that there isn't a character inside the single-quotes. That's not allowed in C.
In your code:
Code: [Select]
if (notes == '') you're basically comparing with nothing.

That's an interesting innovation we can live without: soothing and comforting music when the fire is raging.
Music to die for/by?