-1
int pirsensor = 0;
const int ledpin=11; 
const int soundpin=A2;
const int threshold=200;

void setup() 
{
   pinMode(13, OUTPUT);
   pinMode(2,INPUT);

   Serial.begin(9600);

   pinMode(ledpin,OUTPUT);
   pinMode(soundpin,INPUT);
}

void loop();

const int soundsens = analogRead(soundpin);

if (soundsens >= threshold);
{
    pirsensor = digitalRead(2);
    if (pirsensor == HIGH)
    {
       digitalWrite(13,HIGH);
       digitalWrite(ledpin,HIGH);
    }
    else
    {
        digitalWrite(13,LOW);
        digitalWrite(ledpin,LOW);
    }

    delay(10);
} 

delay(2);
}

The error code is copied below:

testcodefor_soundsensorandPIR:20:5: error: expected unqualified-id before 'if'
     if (soundsens>=threshold,pirsensor = HIGH);
     ^~

testcodefor_soundsensorandPIR:21:1: error: expected unqualified-id before '{' token { ^ testcodefor_soundsensorandPIR:36:10: error: expected constructor, destructor, or type conversion before '(' token delay(2); ^

testcodefor_soundsensorandPIR:37:1: error: expected declaration before '}' token } ^

> exit status 1 expected unqualified-id before 'if'

note this is my attempt to combine codes I was never taught coding

jsotola
  • 2,542
  • 3
  • 13
  • 21
  • Your loop() function isn't a function. You've declared it but not defined it, because you put a semi-colon on the end, and then you forgot to use an opening brace { to start it. – brhans Apr 08 '22 at 00:33
  • You can't separate 2 expressions in an if statement like that. If you want to evaluate different things you need to use the boolean operators like && and ||, and you can't end an if with a semi-colon. – brhans Apr 08 '22 at 00:36
  • And although the compiler doesn't care about the way you indent or space your code (or not), being consistent about it makes it so much easier to read and debug. – brhans Apr 08 '22 at 00:40
  • 3
    And lastly (from me anyway) this is a pure C coding question and belongs on stackoverflow. – brhans Apr 08 '22 at 00:44

3 Answers3

1

That if statement does not make sense. Remove ,pirsensor = HIGH and the semicolon at the end.

Maybe it’s time to do some tutorials on C/C++ and coding. Randomly stringing bits of code together is like playing random notes on a piano. You wont be an expert overnight, but a little knowledge will move you forward.

Kartman
  • 6,258
  • 2
  • 7
  • 13
  • The PIR line was not supposed to be there I fixed it to how it was originally. Any suggestions for well explained tutorials? –  Apr 08 '22 at 00:37
  • Sorry, i’ve not needed to watch tutorials on Arduino and C/C++, so I can’t help there. There’s got to be zillions to choose from. – Kartman Apr 08 '22 at 01:03
0

Delete the semicolon highlighted below in yellow, delete semicolon crossed in red and add semicolons added in red.

enter image description here

GT Electronics
  • 4,098
  • 8
  • 15
-1

First of all, make sure the brackts are corrected for Loop function.

Second: you cannot use "," inside and if function (misuse of the term funtion to if, as pointed by TypeIA). If you want to use multiple conditions like "this and that" type && if you want to use "this or that" use ||.

Third: Do not use "=" with "if", if you want to compare if something is equal than other use "=="

This example shows a "OR" logic to the if function

void loop() //note that there isn't a ";" after ()

{

const int soundsens=analogRead(soundpin);

if (soundsens>=threshold || pirsensor == HIGH) // if soundsens is bigger or equal threshold OR pirsensor is equal to high, than if is true

{

```

Gimenez
  • 154
  • 4
  • This answer is wrong in a few ways. Aside from grammar, "if" is not a "function," it's not true that it only accepts "logical values" (at least in C), it can accept = (best used never or only carefully and conscientiously). -1 – TypeIA Apr 08 '22 at 06:02
  • Sorry for the bad grammar, i didn't know that perfect english is required to try to help someone in electronics topics. But i do agree with you, the term "function" for "if" is incorrect and a appreciate you pointing that. As to the use of "=" in if i still don't agree. = is an assignment operator and == is a comparator operator. The whole point of if is to compare – Gimenez Apr 08 '22 at 12:53