Discover Arduino

Hi everyboy :slight_smile:

I'm discovering Arduino and its programmation and i'm coding a simple code to test Arduino Uno. But i don't understand why it doen't work. I already code in C and it's pretty similar. Here's my code:

#define P_PV A0
#define P_R A1
#define P_C A2
#define K_PV 2
#define K_R 3

int PV, C;

void setup()
{
pinMode(P_PV, INPUT);
pinMode(P_R, INPUT);
pinMode(P_C, INPUT);
pinMode(K_PV, OUTPUT);
pinMode(K_R, OUTPUT);

digitalWrite(K_R, LOW);
digitalWrite(K_PV, LOW);
}

void loop()
{
PV=analogRead(P_PV);
C=analogRead(P_C);

if(PV>C)
{
digitalWrite(K_R, HIGH);
digitalWrite(K_PV, LOW);
}

else
{
digitalWrite(K_R, LOW);
digitalWrite(K_PV, LOW);
}
}

No need to explain it, i'm just using a if(). But i don't know why, i can compil and transfer the code to Arduino but digital outputs aren't work in function of the analogic inputs. Can you help me?

Thanks :slight_smile:

How do you know the outputs aren’t correct?

I’d suggest you use Serial to debug, perhaps printing the values as it reads them, and then printing a message to say which part of the if it is in. See my <<<<<<< comments below.

#define P_PV A0
#define P_R A1
#define P_C A2
#define K_PV 2
#define K_R 3

int PV, C;

void setup()
{                
  pinMode(P_PV, INPUT); 
  pinMode(P_R, INPUT); 
  pinMode(P_C, INPUT);  
  pinMode(K_PV, OUTPUT);     
  pinMode(K_R, OUTPUT);
  
  digitalWrite(K_R, LOW);
  digitalWrite(K_PV, LOW);  

Serial.begin(9600); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<
Serial.println("Starting...."); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}


void loop()
{
    PV=analogRead(P_PV);
Serial.println(PV); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    C=analogRead(P_C);
Serial.println(C); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<
  
    if(PV>C)
    {
Serial.println("In the PV > C part of the if"); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      digitalWrite(K_R, HIGH);
      digitalWrite(K_PV, LOW);
    }
    
    else
    {
Serial.println("In the else part"); //<<<<<<<<<<<<<<<<<<<<<<<<<<<<
      digitalWrite(K_R, LOW);
      digitalWrite(K_PV, LOW);
    } 
}

No need to explain it

Yes, there is. What is attached to the analog pins?

Analog pins are INPUT only, so there is no need to use the pinMode() statement for analog pins.

int PV, C;

Why are these variables, used only in loop(), global?

    if(PV>C)
    {
      digitalWrite(K_R, HIGH);
      digitalWrite(K_PV, LOW);
    }
   
    else
    {
      digitalWrite(K_R, LOW);
      digitalWrite(K_PV, LOW);
    }

If PV is greater than C, set the K_PV pin LOW. Otherwise, set the K_PV pin LOW. I see.

Ok JimboZA, I will. I can't do it now because I don't have the materials to make it but it will be efficient to found the bug with this function.

PaulS

I just put 2 voltage generator in the analog inputs. I change the voltage of its between 0 and 5V to test the if(). After I watch the digital outputs on an oscilloscope.

For pinMode() use for the analog inputs, I know they are inputs but I don't know very well Arduino and use pinMode() for analog inputs was a kind of security. Useless but I put it to be sure.

About the PV and C variables, I don't understand where you want to use it? They're just useful in the loop()

If PV is greater than C, set the K_PV pin LOW. Otherwise, set the K_PV pin LOW. I see.

One more time, simple security, I can't make a simple code works on Arduino, so when I'll make a good code, I'll erase useless lines one by one and test if I really can do it.

I just put 2 voltage generator in the analog inputs. I change the voltage of its between 0 and 5V to test the if().

You do not say what form these voltage generators take but do they have a common GND with the Arduino ?

Arf... It must be that... I made a common GND but I forget to put this GND on a GND pin from Arduino...

I guess no need to link the GND from digital I/O to analog inputs GND? They're already linked?

They're already linked?

Yes.

Ok JimboZA, I will. I can’t do it now because I don’t have the materials to make it but it will be efficient to found the bug with this function.

You don’t need any materials to do what I suggested: Serial output is to the IDE monitor (contril-shift-M) through the usb cable.