Search code examples
cfunctionarduinoarduino-idearduino-c++

my function-definition seems not get included in void loop


I was practicing and trying my Analog to Digital Converter and trying this simple test. And here is my code :

#include <Adafruit_MCP3008.h>

Adafruit_MCP3008 adc;

  float voltage(int raw){
  return raw / 1023 * 4.9;
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  Serial.println("MCP3008 simple test.");

  adc.begin();
  
}

void loop() {
  int raw = adc.readADC(0);

  Serial.print(raw);
  Serial.print("\t");
  Serial.println(voltage(raw));

  delay(1000);
}

and when I opened my serial monitor it only showing the changes of "raw", but my "voltage" function seems not get included, here is what I got, as you can see it only shows the result for raw (on left), but not showing the voltage (on right) serial monitor

I'm trying to make sure I got the "voltage" function affected also inside the void loop. can anybody explain me the rule of program in this case ?


Solution

  • I think you have a conversion problem. You are passing a int and divide it by 1023. This would give you 0, then multiplied by 4.9 would give you 0.0

    You should cast your int raw into a float.