Code is not error but the function is not working

Hello everyone, I typed in this code
void setup() {
// put your setup code here, to run once
pinMode(8, OUTPUT);
pinMode(5, OUTPUT);
Serial.begin(9600);
pinMode(3, OUTPUT);
}

void loop(){
if (Serial.println(analogRead(A0)));
delay(1000);
{
(tone (8, 494, 250));
delay(325);
tone(8, 440, 250);
delay(325);

{
while (1)
{
digitalWrite(5, HIGH);
delay(200);
digitalWrite(5, LOW);
delay(200);
}}}

{
digitalWrite(5, LOW);
}}

Any ideas about the issue of the code?

Welcome to the forum

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

Please post your sketch, using code tags when you do. This prevents parts of it being interpreted as HTML coding and makes it easier to copy for examination

In my experience the easiest way to tidy up the code and add the code tags is as follows
Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

What is the code supposed to do ?
What have you got connected to the Arduino ?

What is this supposed to do? Note the semicolon.
if (Serial.println(analogRead(A0)));

You did not describe the issue :wink:

Please edit your post, select all code and click the <CODE/> button; next safe your post. Thies will make it easier to read the code, easier to copy and the forum software will display it properly.

Even without the semicolon that line of code is meaningless as Serial.prinln() will never return a value of zero so the statement will never be anything other than true

I sense a rite of passage in The Farce.

First, follow the instructions on how to post correctly and redo your topic.

Then, I corrected your code and tested it,
but the initial if statement doesn't make any sense.
and the last statement too .

The rest works.
The buzzer sounds and then the LED blinks indefinitely.

void setup() {
  pinMode(8, OUTPUT);
  pinMode(5, OUTPUT);
  Serial.begin(9600);
  pinMode(3, OUTPUT);
}

void loop() {
  if (Serial.println(analogRead(A0))) // ????
    delay(1000);
  (tone (8, 494, 250));
  delay(325);
  tone(8, 440, 250);
  delay(325);
  while (1)  {
    digitalWrite(5, HIGH);
    delay(200);
    digitalWrite(5, LOW);
    delay(200);
  }
  digitalWrite(5, LOW);  // ????
}

the sensor did not sense anything

You didn't answer UkHeliBob's question:

What have you got connected to the Arduino ?

What sensor is it?
Nor SurferTim's question about what this line is supposed to do:

if (Serial.println(analogRead(A0)));

???

    if (Serial.println (analogRead (A0)))   ;
    delay(1000);
    {

in above

  • a semicolon following an if statement becomes the body of the statement, what is done when the condition is true; hence in the case, nothing is done
  • invoking a Serial print within the conditon of an if statement doesn't make sense
  • it appears the the body of the if statement may have been intended as the code after the brace, '{', but the delay(1000) would become the body if the semi colon weren't there.

in above

  • the while 1 traps the code in that loop, presumably turning on/off some LED.

the following is a guess at what is intended and will turn on/off a buzzer and LED when the sensor is > 100

const byte PinLed = 5;
const byte PinBuz = 8;

void setup ()
{
    Serial.begin (9600);

    pinMode (PinBuz, OUTPUT);
    pinMode (PinLed, OUTPUT);
}

void loop ()
{
    int val = analogRead (A0);          // read sensor
    Serial.println (val);               // display sensor value

    if (100 < val)  {                   // check if sensor > some value
        digitalWrite (PinLed, HIGH);    // turn led and buzzer on
        tone (PinBuz, 494, 250);

        delay (325);                    // wait 1/3 sec

        digitalWrite (PinLed, LOW);     // turn led and buzzer off
        tone (PinBuz, 440, 250);

        delay (325);                    // wait again
    }
}