Is there an error in my code?

See subject title, I am trying to make a program that will read an external resistor and display the resistance on an LCD and the serial monitor; however, the resistance is alway read as 0 and nothing displays on the LCD at all. Does my code have an error, or do I need to check my wiring? Note that the code is verified successfully, so there are no compile-time errors.

#include<LiquidCrystal.h>
LiquidCrystal lcd(12,11,5,4,3,2);
const int inPin=A0;
unsigned long resistance=0;
void setup(){
 lcd.begin(16,2);
 Serial.begin(9600);
}
void loop(){
 resistance=calculate(analogRead(inPin));
 Serial.print("Resistance: ");
 Serial.println(resistance);
 lcd.clear();
 lcd.print("Resistance:");
 lcd.setCursor(0,1);
 lcd.print(resistance);
 delay(100);
}
unsigned long calculate(int input) {
 float calc=(1023000/input)-1000;
 unsigned long out=(unsigned long)calc;
}

Do this:

  • File > Preferences > Compiler warnings: All > Show verbose output during > compilation (uncheck) > OK
  • Sketch > Verify/Compile
  • After compilation finishes, scroll the black console window at the bottom of the Arduino IDE all the way to the top and closely examine the red text. Do you see the problem now?

Compiler warnings are extremely useful. You should always have them turned on and should always pay attention to them.

Hi,
Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

Hi,
I think you need to read this tutorial,especially about "returning' variable back from the function.

https://playground.arduino.cc/Code/Function

Tom... :slight_smile:

Crank up the warning level (in Preferences) until you see these warnings:

/Users/john/Downloads/proj1_487/proj1_487.ino: In function 'long unsigned int calculate(int)':
/Users/john/Downloads/proj1_487/proj1_487.ino:21:17: warning: unused variable 'out' [-Wunused-variable]
   unsigned long out = (unsigned long)calc;
                 ^
/Users/john/Downloads/proj1_487/proj1_487.ino:22:1: warning: no return statement in function returning non-void [-Wreturn-type]
 }
 ^

Those mean that you forgot to put "return out;" at the end of your 'calculate' function.

How is the resistor wired?

Never mind, I'm changing it a bit, but the

return

was helpful!