Coding errors with Arduino nano

Hello,
I try to code using Serial.println a message on monitor, and using 2 buttons to activate 2 outputs of Arduino nano, but I got the errors. Could you please verify why it is ? Thanks.

// Arduino Nano

#include <SoftwareSerial.h>

void setup() {
  Serial.begin(9600);
  
  // INPUTS (D3,D4,D5)
  const int LPI      = D3;
  const int FAN_VAL  = D4;

  pinMode(LPI,      INPUT);    
  pinMode(FAN_VAL,  INPUT);      
       
 
  // SWITCHES READING (A0, A1, A2)
  const int SW1 = A0;   
  const int SW2 = A1;    
  
  pinMode(SW1, INPUT_PULLUP); 
  pinMode(SW2, INPUT_PULLUP);


  // OUTPUTS (D10, D11, D12) @ (SW1, SW2, SW3)
  const int ONF_HEAT = D12; 
  const int ONF      = D11; 

  pinMode(ONF_HEAT, OUTPUT);    
  pinMode(ONF,      OUTPUT);      

}

void loop() {

  if (int digitalRead(LPI) == 1) {
    Serial.println("1. LPI = +5V");
  }
  else {
    Serial.println("1. LPI = 0V");
  }
  delay (3000);

  if (int MP = digitalRead(MPI) == 1) {
    Serial.println("2. MPI = +5V");
  }
  else { 
    Serial.println("2. MPI = 0V");
  }
  delay (3000);

 
  Serial.println ("VERIFY 3 INPUTS @ SW1, SW2");

  // Reading Status SW1
  if (int S1 = digitalRead(SW1) == 0) {
    digitalWrite(ONF_HEAT, HIGH);
  }
  delay (3000);
  
  // Reading Status SW2
  if (int S2 = digitalRead(SW2) == 0) {
    digitalWrite(ONF, HIGH);
    delay (3000);
  }
// return;
}

The Arduino pins as this photo:

Thanks for replying,
V5D

Kind of errors:

On regular Nano code change "Dx" to "x" (for example "FAN_VAL = D4" >> "FAN_VAL = 4"... you will have more errors to correct... and you need to post those errors, too, but post them in a "code block". Your new errors will be from not defining variables in the right scope (defining inside a function restricts scope to that function).

Look more closely at just about any working example. You do not declare constants for pin numbers, for example, within the body of setup, because then they only exist there, and cannot be referenced elsewhere.
Again, please go look at, and mimic, the structure of simple examples.

+1.

Sometimes I feel like a human AI, the extent to which pattern matching and common sense underlie most of my success in this hobby. Maybe even in life itself. :expressionless:

There is nothing like a working example to answer questions definitively.

a7

you need to cleanup the mess of your pin definitions. and a digitalRead is not "int" it is a "bool" statement.

if (int MP = digitalRead(MPI) == 1) {
    Serial.println("2. MPI = +5V");
  }

would work better like this:

if(digitalRead(MPI)) {
Serial.println("2. MPI = +5V");
  }

in the if condition you can omit "== 1" for checking if it is true. If you want to check a false state you can do it like this:
if( !digitalRead(PIN) )
... just a tiny contribution from my side :wink:

actually I don´t see MPI declared neither as a variable nor as a Pin.

One way to comply with the API for digitalRead():

  if (digitalRead(MPI) == HIGH) {
    Serial.println("2. MPI = +5V");
  }

and you have to

if (digitalRead(PIN) == LOW)

or

if (digitalRead(PIN) != HIGH)

as well.

Defining constants makes for compliance and readability:

// up top

# define PRESSED HIGH

// and eveywhere else

  if (digitalRead(MPI) == PRESSED) {
    Serial.println("2. MPI = +5V");
  }

a7

Thanks for all with your replying.
The problem is sold when the "constant int" are moved out of the void setup()...
V5D

Could I use A0 as Digital Input ?

const int SW1 = A0;             // Reserved. R9 installed, testing ONF_HEAT (sending +5V) when not connect to DPM
pinMode(SW1, INPUT_PULLUP);

because I can't get this pin = +5V (HIGH).

Thanks.


Yes.

Okay, I have a question about this, too. The statement "const int SW1 = A0;" could generate an error of syntax because the symbol 'A' is not an int. How does the compiler treat this?

See pins_arduino.h:

#define PIN_A0   (14)
#define PIN_A1   (15)
#define PIN_A2   (16)
#define PIN_A3   (17)
#define PIN_A4   (18)
#define PIN_A5   (19)
#define PIN_A6   (20)
#define PIN_A7   (21)

static const uint8_t A0 = PIN_A0;
static const uint8_t A1 = PIN_A1;
static const uint8_t A2 = PIN_A2;
static const uint8_t A3 = PIN_A3;
static const uint8_t A4 = PIN_A4;
static const uint8_t A5 = PIN_A5;
static const uint8_t A6 = PIN_A6;
static const uint8_t A7 = PIN_A7;

I have 3 switches for A0, A1, A2 pins:

  const int SW1 = A0;     
  const int SW2 = A1;    
  const int SW3 = A2; 

  pinMode(SM1, INPUT_PULLUP); 
  pinMode(SW2, INPUT_PULLUP); 
  pinMode(SW3, INPUT_PULLUP);

pin A1 = pin A2 = +5V, but pin A0 = 0V. Yes, it's compiled without errors.

I have changed the Arduino nano, got the same status on A0 !

ok, but I can't use the Input_pullup resistor. Maybe have to use an external resistor pullup.

Why not?

But yes, input pins with pushbuttons attached have to be pulled somehow.

It will be up,if you use the internal pull-up, and your button will make the pin LOW when it is pressed (normally open contacts).

You can use an external resistor to,pull either way. It is always advised to use pullup (resistor between 5 volts annd the input pin) and wire the contacts between the input pin and ground.

a7

Ummm. The const ints are SWX, but your pinModes are SMX. Problem?

Sorry typo-error :wink: But that is an important note ! It's OK now. Thanks !

Still not answered why. The pullups work just fine on my Nano clones, right up through D19/A5.

I try to use as:

  const int SW1 = 14;     // pin A0
  const int SW2 = 15;    // pin A1   
  const int SW3 = 16;    // pin A2

It's also OK. Thanks for replying.

You can use pullups with analog pins equally you can with digital pins.