I don't know what's wrong with the code

So I recently started Arduino and I am doing Arduino Analog input and output on LED & Serial.
So when I verify the code, it's saying: 'inputVal' was not declared in this scope (I'll upload the code)
Can anyone help?
Code: /*

Tutorial for Analog input and Output.
 Input through Potentiomete and output through PWM on LED and serial monitor.
 Prepared by Robo India.
 www.roboindia.com
 */
int analog_ip = A7;
int LED = 3;
int intputVal = 0;
int old_ip_val = 0;

void setup() {
  pinMode (LED, OUTPUT);
  Serial.begin(96000);
  Serial.print("ROBO INDIA\nroboindia.com/nTutorial on Analog inout and output.\n");
}

void loop() {
inputVal = analogRead(analog_ip);
  if (inputVal < old_ip_val - 5 | | inputVal < old_ip_val + 5)
}

{
  Serial.print("Input Value    : ");
    Serial.print(inputVal);
    Serial.print("\nLED Brightness : ");
    Serial.print(inputVal / 4);
    Serial.print("\n");
    analogWrite(LED, inputVal / 4);
    delay(500);
    old_ip_val = inputVal;

{
  }
}
Error Message: Arduino: 1.8.19 (Mac OS X), Board: "Arduino Uno"











/Users/anshumansrivastava/Desktop/Analog_input_and_output_on_LED_n_Serial/Analog_input_and_output_on_LED_n_Serial.ino: In function 'void loop()':
Analog_input_and_output_on_LED_n_Serial:19:1: error: 'inputVal' was not declared in this scope
 inputVal = analogRead(analog_ip);
 ^~~~~~~~
/Users/anshumansrivastava/Desktop/Analog_input_and_output_on_LED_n_Serial/Analog_input_and_output_on_LED_n_Serial.ino:19:1: note: suggested alternative: 'intputVal'
 inputVal = analogRead(analog_ip);
 ^~~~~~~~
 intputVal
/Users/anshumansrivastava/Desktop/Analog_input_and_output_on_LED_n_Serial/Analog_input_and_output_on_LED_n_Serial.ino: At global scope:
Analog_input_and_output_on_LED_n_Serial:21:2: error: expected unqualified-id before '{' token
  { if( inputVal<old_ip_val-5 || inputVal<old_ip_val+5)
  ^
Analog_input_and_output_on_LED_n_Serial:25:1: error: expected unqualified-id before '{' token
 {
 ^
exit status 1
'inputVal' was not declared in this scope


This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

(I am on macOS Monterey 12.2.1

Screenshots of code are nearly worthless. We cannot copy and paste them into an editor or the IDE for examination and verification. Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please include the entire error message. It is easy to do. There is a button (lower right of the IDE window) called "copy error message". Copy the error and paste into a post in code tags. Paraphrasing the error message leaves out important information.

If you read and follow the guidelines you will get better and faster help.

Bracketing is not right. The 'if' loop does not have any staring bracket. Has a closing bracket only. The if loop has to be like the following-

if( inputVal<old_ip_val-5 || inputVal<old_ip_val+5)
{
//Whatever task you want to perform
}
int intputVal = 0;
inputVal = analogRead(analog_ip);

intputVal is not the same as inputVal.

There is no such structure as an if loop.

intputVal isn’t the same as inputVal

You need to think about WHERE you put your posts…

Once you fix the inputVal issue,
Here is your loop fixed. There were extra brackets and a space between | | that should not be there. It needs to be || (no space).

void loop()
{
   inputVal = analogRead(analog_ip);
   // *********** no space between ||
   if (inputVal < old_ip_val - 5 || inputVal < old_ip_val + 5)
      //} ********** extra bracket
   {
      Serial.print("Input Value    : ");
      Serial.print(inputVal);
      Serial.print("\nLED Brightness : ");
      Serial.print(inputVal / 4);
      Serial.print("\n");
      analogWrite(LED, inputVal / 4);
      delay(500);
      old_ip_val = inputVal;
      //{  ********** extra bracket
   }
}

Here is the current code I did regarding replies

/*
  Tutorial for Analog input and Output.
  Input through Potentiomete and output through PWM on LED and serial monitor.
  Prepared by Robo India.
  www.roboindia.com
*/
int analog_ip = A7;
int LED = 3;
int intputVal = 0;
int old_ip_val = 0;

void setup() {
  pinMode (LED, OUTPUT);
  Serial.begin(96000);
  Serial.print("ROBO INDIA\nroboindia.com/nTutorial on Analog inout and output.\n");
}

void loop() {
  inputVal = analogRead(analog_ip);
  if ( inputVal < old_ip_val - 5 || inputVal < old_ip_val + 5)

  {
    Serial.print("Input Value    : ");
    Serial.print(inputVal);
    Serial.print("\nLED Brightness : ");
    Serial.print(inputVal / 4);
    Serial.print("\n");
    analogWrite(LED, inputVal / 4);
    delay(500);
    old_ip_val = inputVal;


  }
}

Missed one.

It still says wrong. I'm new so I don't understand why it's wrong.

Look carefully. The variable names must match, exactly.

This compiles:

/*
  Tutorial for Analog input and Output.
  Input through Potentiomete and output through PWM on LED and serial monitor.
  Prepared by Robo India.
  www.roboindia.com
*/
int analog_ip = A7;
int LED = 3;
int inputVal = 0;  // no t
int old_ip_val = 0;

void setup() 
{
  pinMode (LED, OUTPUT);
  Serial.begin(96000);
  Serial.print("ROBO INDIA\nroboindia.com/nTutorial on Analog inout and output.\n");
}

void loop()
{
  inputVal = analogRead(analog_ip);
  if ( inputVal < old_ip_val - 5 || inputVal < old_ip_val + 5)

  {
    Serial.print("Input Value    : ");
    Serial.print(inputVal);
    Serial.print("\nLED Brightness : ");
    Serial.print(inputVal / 4);
    Serial.print("\n");
    analogWrite(LED, inputVal / 4);
    delay(500);
    old_ip_val = inputVal;
  }
}

ok, I made the required correction of the variables. For some reason it says still something's wrong

The code that I included my last post compiles for me.

Then you should post the code and post the entire error message.

you do need to use the same name, arduino doesnt spell check.

Also you should sort out your braces, this is very messy. I see youre just checking the first part of the loop(); but just cut and paste the "junk" into notepad rather then leave it in your code!

Your topic was MOVED to its current forum category as it is more suitable than the original

Hello! I´m just curious. You don´t need to answer at all.
Have 2 questions:

  1. Is it a valid port?
    int analog_ip = A7;
    for : Board: "Arduino Uno"?

  2. The second comparison should be a ">" instead of a "<"?
    if (inputVal < old_ip_val - 5 || inputVal < old_ip_val + 5)

Good luck everybody! :grinning:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.