Fresh eyes for issues with my PIR and sound detection combination sketch

Hello Arduino Peeps :slight_smile:

I'm trying to add a sound detector to this SparkFun Spooky Sketches - MP3 Trigger - YouTube by modifying this sketch Spooky_Sketches/SparkFunSpookySketchSound.ino at master 路 sparkfun/Spooky_Sketches 路 GitHub

Going by the error I "get two or more data types in declaration of 'loop'" it seems that adding code for the sd sound detector code is wrong somewhere but I can't see where....

int pirPin = 2; //digital 2 is motion sensor pin
int sdPin = 3; //digital 3 is sound detector pin

bool oldState;
int showType = 0; int

void setup() {
聽 // put your setup code here, to run once:
Serial.begin(9600); 
pinMode(pirPin, INPUT); //reciving info from motion sensor pin
pinMode(sdPin, INPUT); //reciving info from sound detector pin
pinMode(9, INPUT); 
pinMode(10, INPUT); 
pinMode(11, INPUT); 
pinMode(12, INPUT); 
pinMode(13, INPUT); 
digitalWrite(9, LOW); //trigger off
digitalWrite(10, LOW); //trigger off
digitalWrite(11, LOW); //trigger off
digitalWrite(12, LOW); //trigger off
digitalWrite(13, LOW); //trigger off
}

void loop() {
聽 // put your main code here, to run repeatedly:
int pirVal = digitalRead(pirPin); //read the motion sensor value
int sdVal = digitalRead(sdPin); //read the sound detector value
Serial.print(pirVal);
Serial.print(sdVal);
Serial.print(" ");
Serial.print(showType);
Serial.println(" ");


if(pirVal == LOW && oldPirState == HIGH){ //was motion detected 
if(sdVal == LOW && oldSdState == HIGH){ //was sound detected
showType++; //switch to the next case
if (showType > 4) //if over 4
showType=0; //set it back to 0 
startShow(showType); //run the case
pinMode(9, INPUT); 
pinMode(10, INPUT); 
pinMode(11, INPUT); 
pinMode(12, INPUT); 
pinMode(13, INPUT); 
digitalWrite(9, LOW); //trigger off
digitalWrite(10, LOW); //trigger off
digitalWrite(11, LOW); //trigger off
digitalWrite(12, LOW); //trigger off
digitalWrite(13, LOW); //trigger off
}
oldPirState = pirVal;
oldSdState = sdVal;
}


void startShow(int i) {
switch(i){
case 0: pinMode(9, OUTPUT);
digitalWrite(9, LOW); //first sound trigger
delay(2000);
break;
case 1: pinMode(10, OUTPUT);
digitalWrite(10, LOW); //second sound trigger
delay(2000);
break;
case 2: pinMode(11, OUTPUT);
digitalWrite(11, LOW); //third sound trigger
delay(2000);
break;
case 3: pinMode(12, OUTPUT);
digitalWrite(12, LOW); //forth sound trigger
delay(2000);
break;
case 4: pinMode(13, OUTPUT);
digitalWrite(13, LOW); //fifth sound trigger
delay(2000);
break;

}
}

And I get this error

sketch_may28a:36: error: two or more data types in declaration of 'loop'
 void loop() {
聽 聽 聽 聽 聽  ^
/Users/Documents/Arduino/sketch_may28a/sketch_may28a.ino: In function 'void loop()':
sketch_may28a:47: error: 'oldPirState' was not declared in this scope
 if(pirVal == LOW && oldPirState == HIGH){ //was motion detected 
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽  ^
sketch_may28a:48: error: 'oldSdState' was not declared in this scope
 if(sdVal == LOW && oldSdState == HIGH){ //was sound detected
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 ^
sketch_may28a:52: error: 'startShow' was not declared in this scope
 startShow(showType); //run the case
聽 聽 聽 聽 聽 聽 聽 聽 聽  ^
sketch_may28a:65: error: 'oldSdState' was not declared in this scope
 oldSdState = sdVal;
 ^
sketch_may28a:69: error: a function-definition is not allowed here before '{' token
 void startShow(int i) {
聽 聽 聽 聽 聽 聽 聽 聽 聽 聽 聽  ^
sketch_may28a:93: error: expected '}' at end of input
 }
 ^
exit status 1
two or more data types in declaration of 'loop'

Can anyone advise me what I'm doing wrong before I go blind looking at this?

Thx
Emma

Take a closer look at line 5 :wink:

Also, I know your real name now, you might want to remove that from the error-log.

lg, couka

error: 'oldPirState' was not declared in this scope

Well, where was oldPirState declared?

couka:
Take a closer look at line 5 :wink:

Also, I know your real name now, you might want to remove that from the error-log.

lg, couka

Hey couka
I still can't see it, I have really no idea what I'm missing, Can you give me another clue lol?
Thx for that btw, its my dads comp.

Emma

There's an "int" after the semicolon for no reason.

lg, couka

Can鈥檛 believe I missed that, I thought I was going blind
Sadly it doesn鈥檛 solve my problem :frowning:

EmmaLK:
Sadly it doesn't solve my problem :frowning:

To be precise: It doesn't solve all of them. But the other error messages are relatively easy to understand, so read them.

lg, couka

Hi,

oldPirState = pirVal;[color=#222222][/color]
oldSdState = sdVal;

You have not declared what type of variable oldPirState and oldSdStare are.
That is what the error log is saying.
What type of variable are they, int, bool, byte or whatever.

Tom... :slight_smile:

Hi Tom
I thought you meant to add 鈥渂ool鈥 where I missed it before?

bool oldPirState = pirVal;
bool oldSdState = sdVal;

But now the error tells me 鈥榮tartShow鈥 was not declared in this scope
I don鈥檛 know how to declare it, can you give me some advice?

Thx
Emma

Hi,

https://www.arduino.cc/en/Reference/VariableDeclaration

This should help, Tom.. :slight_smile:

Emma--

There are problems with your curly braces. In order to detect problems with missing and extra braces, I recommend that each curly brace be on its own line like

void loop()
{
//loop code here
}

or

if (pirVal == LOW && oldPirState == HIGH) 
{
//conditional statement here
}

Use of the auto format tool (Ctrl T) will help in lining braces up. The IDE will also highlight the matching brace when you click next to one of the pair.

Go through your code again with focus on the curly braces.

cattledog

Thank you!!!
I guess that is typical noob mistake I made

cattledog:
Emma--

There are problems with your curly braces. In order to detect problems with missing and extra braces, I recommend that each curly brace be on its own line like

void loop()

{
//loop code here
}




or 



if (pirVal == LOW && oldPirState == HIGH)
{
//conditional statement here
}





Use of the auto format tool (Ctrl T) will help in lining braces up. The IDE will also highlight the matching brace when you click next to one of the pair.

Go through your code again with focus on the curly braces.