if condition not working based on voltage reading (desperate for solution)

Hi guys I desperately need your help in solving, what looks like, a very trivial issue. Today we spent over 7 hours debugging the hardware and software for our project and have narrowed the problem down to something that is truly mind boggling to us. the code below is a snippet of the whole project, but its is the main issue no doubt.

float sensorValue;
float voltage;
float x = 2.0;//some voltage threshold

void setup() {

  Serial.begin(9600);
  pinMode(4, OUTPUT);
}


void loop() {
  
  sensorValue = analogRead(A0);
  
  voltage = sensorValue * (5.0 / 1023.0) * 5;//mult by 5 b/c sensor divides actual sensed voltage by 5
                                                             //to prevent damage and return that value to us
  Serial.println(voltage);
  if(voltage <= x){
      digitalWrite(4,HIGH);  
    //delay(50);
  }else if(voltage > x){
      digitalWrite(4,LOW);  
      //delay(50);
    
  }
  
  
}

the voltage sensor in question is this
http://www.emartee.com/product/42082/Voltage%20Sensor%20Module%20%20Arduino%20Compatible

we have confirmed that the sensor is working correctly. We are driving leds using mostfets as switches through digital pins and they are working correctly as well, so long as the if statements are taken out, and a delay is added for a blinking effect, BUT AS SOON AS WE TRY AND MAKE A DECISION OF WHEN TO TURN ON THE LEDs BASED ON THE VALUE FROM THE SENSOR, IT STOPS WORKING. the leds are either fully on or off, there is no response to the sensed value/if statements.

If you need to see the circuit please let me know I can quickly make one, but again it works as a simple blinking exercises as long as the if statements are gone. The sensor reads and displays the correct voltage, which we are applying and changing manually.

I don't see how this is possible, can someone please help us!

Desperate haha

You do not have any blink function code.

If the voltage is over x, you turn the output on but don't turn it off,on,off etc.

If under x, you turn it off but don't blink.

The else if line does not require the test part as it only gets there if it is true from the previous if test.

Weedpharma

It's very likely that I'm missing something here, because for me that snippet works exactly as expected.

I don't have your sensor so I hooked a pot up to A0 with 5V across the ends, and took out your 5x factor.

I added some Serial.print() in the if and the else so we can see where the logic goes. The output below shows the logic works, and the LED (I changed to 13) is on or off as expected. Added a 1000ms delay so we can read the serial output at reasonable speed.

What should that snippet do, other than turn the LED off or on? I'm clearly missing something from your explanation... You refer to "second value / if statements".... what's that all about?

starting.....
1.43 in the if
1.72 in the if
1.90 in the if
2.32 in the else
2.34 in the else
2.43 in the else
2.56 in the else
2.71 in the else
2.30 in the else
1.91 in the if
1.74 in the if
1.49 in the if
float sensorValue;
float voltage;
float x = 2.0;//some voltage threshold

void setup() {

  Serial.begin(9600);
  Serial.println("starting.....");
  pinMode(13, OUTPUT);
}
void loop() {

  sensorValue = analogRead(A0);

  voltage = sensorValue * (5.0 / 1023.0);
  Serial.print(voltage);
  if(voltage <= x){
    digitalWrite(13,HIGH); 
    Serial.println(" in the if"); 
    //delay(50);
  }
  else if(voltage > x){
    digitalWrite(13,LOW);
    Serial.println(" in the else");  
    //delay(50);
  }
 delay(1000); 
}

The code above works exactly as expected.....

weedpharma:
You do not have any blink function code.

If the voltage is over x, you turn the output on but don't turn it off,on,off etc.

If under x, you turn it off but don't blink.

The else if line does not require the test part as it only gets there if it is true from the previous if test.

Weedpharma

Thanks for the reply weed.

I don't want it to blink, I tested blink and it works. I am controlling the voltage, the sensor is reading it and I'm trying to thrown the switch based on the voltage reading, but it's not doing that, only God knows why.

JimboZA:
It's very likely that I'm missing something here, because for me that snippet works exactly as expected.

I don't have your sensor so I hooked a pot up to A0 with 5V across the ends, and took out your 5x factor.

I added some Serial.print() in the if and the else so we can see where the logic goes. The output below shows the logic works, and the LED (I changed to 13) is on or off as expected. Added a 1000ms delay so we can read the serial output at reasonable speed.

What should that snippet do, other than turn the LED off or on? I'm clearly missing something from your explanation... You refer to "second value / if statements".... what's that all about?

starting.....

1.43 in the if
1.72 in the if
1.90 in the if
2.32 in the else
2.34 in the else
2.43 in the else
2.56 in the else
2.71 in the else
2.30 in the else
1.91 in the if
1.74 in the if
1.49 in the if







float sensorValue;
float voltage;
float x = 2.0;//some voltage threshold

void setup() {

Serial.begin(9600);
  Serial.println("starting.....");
  pinMode(13, OUTPUT);
}
void loop() {

sensorValue = analogRead(A0);

voltage = sensorValue * (5.0 / 1023.0);
  Serial.print(voltage);
  if(voltage <= x){
    digitalWrite(13,HIGH);
    Serial.println(" in the if");
    //delay(50);
  }
  else if(voltage > x){
    digitalWrite(13,LOW);
    Serial.println(" in the else"); 
    //delay(50);
  }
delay(1000);
}




The code above works exactly as expected.....

Thanks for your reply Jimbo. That's exactly it! It should do that, but it doesn't. Its a basic piece of code.

By sensed value/if statement I just meant that it should be turning on and off the led based on the condition and the valued it reads from the sensor, whitch we display on the screen and it matches what we put in.

Man I feel stupid right now, I don't know what else to say.

I'll post a schematic tomorrow please let me know if something jumps out at you, but again just totally stumped

Put some Serial.prints in like I did, just to check that it's actually going into the "if" or the "else" as expected.

My code is your code with trivial changes:

  • LED on 13 not 4
  • Took out the 5x factor
  • Added serial prints

And my hardware differs in that I used a pot on A0 not your sensor.

Perhaps you should post all your code....

Hi, can you post the complete sketch please, if its to big to fit code tags, then attach it.

Tom... :slight_smile:
You have declared outputs and inputs?

  if(voltage <= x){
      digitalWrite(4,HIGH);  
    //delay(50);
  }else if(voltage > x){

What is the alternative?

You don't need the second "if".

IT STOPS WORKING

You need to define what "stops working" means.

Hi,
can you try.

_ voltage = (sensorValue * 5.0) / 1023.0;_

Just a thought...Tom.... :slight_smile:

Thanks guys for all the responses.

[Jimbo]
I will try some print statements inside the if's Jimbo, thanks, my guess is it wont work since if it did then my switches would be working.

[Tom]
Sorry Tom I can't post the whole code, I signed a confidentiality agreement, Final year project with supervisor. Yes I have tried using voltage = (sensorValue * 5.0) / 1023.0; also.

[Nick]
yes technically I don't need the second if but it wont break the code either. It's the if statements that are not working, I know it sounds stupid.

[everyone]
I thought I missed something in the code that is special to Arduino but by all the response this is obviously not the case, simple code is simple code. This leads me to think there is something else going wrong now.

If the forum will allow me I will post a schematic that I was using to test this code with, one of my partners, she drew this up quickly. So as you can see we are applying voltage (voltage source) and reading the voltage using the sensor as described in post one. The pins we use on the Galileo are not important, just assume they match the code. Think of this as a type of bypass network, when the voltage drops below some threshold then we are opening the switch in series with the voltage source and closing the one in parallel, bypassing the voltage source all together. Is there a flaw with this circuit??

Thanks Again for all your help guys and girls!

Hi,
hmmm, its hard without full code, but we will persist.
Just to clarify.
You are trying to make a line of LEDs turn on in a bar graph configuration in response to the input voltage?
You have established that you are getting analog input that varies with the voltage that is fed to it? (0 to 1023?)
You have established that you can turn ON and OFF your LEDs separately?
You problem is when you want to turn on the LEDs with input you have problems?

Tom....... :slight_smile:
Awaiting schematic, (not in fritzy format please.)

I've got nothing to add here, but will emphasise you've got all the info you need to take the next step since post #2

The first thing I'd be doing is setting up a serial monitor of the voltage variable - see what number you're getting...

I'd suggest you're going to get a value always over 'x' (and by the way, why not just call 'x' 'threshold' then your code will read more nicely).

Another way to test this is what Jimbo has done - put a serial print of "in else".

Post #10 is also helpful - if your numbers are good the next step is checking the LEDs

According to the pic you posted in Reply #9, you used digital pin1. That's one of the Serial pins......

Not saying it's necessarily anything to do with your problem, but you can't use pin0 or pin1 in your sketch when you're using them for serial comms, so best you move what's on pin 1 to another pin.

Hi,
What are you powering your LEDs off and where are the current limit resistors for the LEDs, the LEDs are wired the wrong way too.
What are the MOSFETS, you didn't say you were high side switching, that is switching the positive side of the load.
Please re assess your diagram and redraw it.

Try ExpressPCB, free cad for doing schematics, use a box as the Uno.
And as noted earlier, don't use digital pins 0 or 1 as they are part of the programming input.

I hate to ask, what exprience do you have with electronics, arduino, programming and hardware.
What is the course and it level, please.

Tom..... :slight_smile:

Hey Guys sorry for the late reply. We haven taken all of your suggestions and have, I believe, correctly concluded that its the digital pins we were using that caused the problem. We were given access to Intel's Galileo Gen2 boards for this project. The coding and switches work perfectly now, BUT, only when we use digital pin 4 to drive the MOSFET! the other pins can turn it on with a HIGH signal, but can't seem to turn it off, again this is a big surprise to us! We know its going inside the if statements because we can print values from inside of them, but for some very strange reason only Pin 4 can successfully turn on AND off the switch.

Any idea why this is happening? I'm reluctant to say that the board is faulty, could we have burned some of the pins?

Thanks again for all your help, this community is amazing.

from Arduino pages directly

"Short circuits on Arduino pins, or attempting to run high current devices from them, can damage or destroy the output transistors in the pin, or damage the entire Atmega chip. Often this will result in a "dead" pin in the microcontroller but the remaining chip will still function adequately. For this reason it is a good idea to connect OUTPUT pins to other devices with 470Ω or 1k resistors, unless maximum current draw from the pins is required for a particular application."

but this doesn't explain why only pin 4 is working correctly, I mean there is no way we destroyed most other pins haha