Arduino Forum

Using Arduino => Programming Questions => Topic started by: filipsons11 on Dec 13, 2019, 02:29 am

Title: Why is this true?
Post by: filipsons11 on Dec 13, 2019, 02:29 am
Trying to understand what I did wrong.
Using a ESP32 dev kit 1
keeps thinking the Cfsrvalue value is high enough to make the "if" statement true.

thanks.

Code: [Select]

const int Carfsr = 34;  //pin link for car side FSR
const int Phonefsr = 35;  //pin link for phone side FSR
const int QiwirelessI = 32; //pink link for Qi wireless chearger
const int Threeway = 27;  //pin link for 3 way electronic valve
const int PumpL = 26; // pin link for Pump
const int Mswitch = 25; // manual switch

int Cfsrvalue = 0;  // setting value for car side to start at 0
int Pfsrvalue = 0;  // setting value for phone side to start at 0
int ManualS;
int QwirelessA;

void setup()
{
 Serial.begin(115200);
 delay(1000);

 // Setting pins I/O mode
 pinMode(Threeway, OUTPUT);
 pinMode(PumpL, OUTPUT);
 pinMode(Mswitch, INPUT);
 pinMode(QiwirelessI, INPUT);
 pinMode(Carfsr, INPUT);
 pinMode(Phonefsr, INPUT);
 
 // Setting pin state
 digitalWrite(Threeway, LOW);
 digitalWrite(PumpL, LOW);
 digitalWrite(Mswitch, LOW);
 digitalWrite(QiwirelessI, HIGH);


 int adcAttachPin(Carfsr);
}

void loop()

{

 // Manual switch reading
 ManualS = digitalRead(Mswitch);

 // Qi wireless charger current sensor trigger reading
 QwirelessA = digitalRead(QiwirelessI);

 // Presure reading from car side
 Cfsrvalue = analogRead(Carfsr);

 // Presure reading from phone side
 Pfsrvalue = analogRead(Phonefsr);




// if both manual switch and phone is on 
if(  ManualS == HIGH && QwirelessA == LOW)
{
       digitalWrite(Threeway, HIGH);
       delay(10);
       digitalWrite(PumpL, HIGH);     
}

// manual switch pump trigger
if( ManualS == HIGH )
{
       digitalWrite(Threeway, HIGH);
       delay(10);
       digitalWrite(PumpL, HIGH);
}
if( ManualS == LOW )
{
       digitalWrite(Threeway, LOW);
       delay(10);
       digitalWrite(PumpL, LOW);
}

// wireless charger current senese pump trigger
if (QwirelessA == LOW)
{
       digitalWrite(PumpL, HIGH); 
}


// on condtions for car presure
if( Cfsrvalue >= 3000 || Cfsrvalue <= 3800)
{
       digitalWrite(Threeway,HIGH);
       digitalWrite(PumpL, HIGH);   
       delay(1000);
       Serial.println(Cfsrvalue);
}



Serial.println(Cfsrvalue);

Serial.println(ManualS);
Serial.println(QwirelessA);


}
Title: Re: Why is this true?
Post by: Klaus_K on Dec 13, 2019, 02:39 am
You are testing for smaller than or equal 3000.
Title: Re: Why is this true?
Post by: filipsons11 on Dec 13, 2019, 02:46 am
sorry that was something I was trying out, look again.

Title: Re: Why is this true?
Post by: DrAzzy on Dec 13, 2019, 02:48 am
 analogRead only returns values in the range 0-1023

Print the values of those variables that you're getting from analogRead for debugging purposes.
Title: Re: Why is this true?
Post by: filipsons11 on Dec 13, 2019, 02:52 am
analogRead only returns values in the range 0-1023

Print the values of those variables that you're getting from analogRead for debugging purposes.
"Reading an analog value with the ESP32 means you can measure varying voltage levels between 0 V and 3.3 V.

The voltage measured is then assigned to a value between 0 and 4095, in which 0 V corresponds to 0, and 3.3 V corresponds to 4095. Any voltage between 0 V and 3.3 V will be given the corresponding value in between."

https://randomnerdtutorials.com/esp32-adc-analog-read-arduino-ide/

I followed this website guide.
Title: Re: Why is this true?
Post by: Klaus_K on Dec 13, 2019, 03:15 am
Place the print before the if statement to make sure you know what the A/D converter results are.
Title: Re: Why is this true?
Post by: david_2018 on Dec 13, 2019, 03:59 am
Is this the section of code you are asking about?
Code: [Select]

// on condtions for car presure
if( Cfsrvalue >= 3000 || Cfsrvalue <= 3800)
{
       digitalWrite(Threeway,HIGH);
       digitalWrite(PumpL, HIGH);  
       delay(1000);
       Serial.println(Cfsrvalue);
}


The if statement will be true if Cfsrvalue is >= 3000 OR Cfsrvalue is <= 3800.  How can that ever be false?  One or the other condition will always be true, for values from 3000 to 3800 both will be true.  For the if statement to be false, both conditions must be false.
Title: Re: Why is this true?
Post by: ToddL1962 on Dec 13, 2019, 07:00 am
It looks like you were intending to call this function but what you really did was declare a function prototype so this line of code does nothing.

Code: [Select]
int adcAttachPin(Carfsr);
Title: Re: Why is this true?
Post by: TomGeorge on Dec 13, 2019, 01:01 pm
Hi,
I think you wanted;

Code: [Select]
// on condtions for car presure
if( Cfsrvalue >= 3000 && Cfsrvalue <= 3800)
{
       digitalWrite(Threeway,HIGH);
       digitalWrite(PumpL, HIGH);  
       delay(1000);
       Serial.println(Cfsrvalue);
}

&&    AND,   not ||   OR, for Cfsrvalue to be true between   3000 and 3800.

Tom.... :)
Title: Re: Why is this true?
Post by: filipsons11 on Dec 14, 2019, 01:45 am
Ahh yes I see now, looks like this fixed the issue.

and yes the int adcAttachPin code was for testing as I thought it was conflicting for the pin, and I read that command clear all others.

Thanks

this helped immensely