Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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 ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading