How to Y axis integers only and no fractions

Arduino Uno. IDE 2.3.4
ReadAnalogVoltage sketch with modification to get Y axis 0-5

Please may I know how to make Y axis the numbers integers only and no fractions? Right now the trace is all over the place, 4.032, 3.001 and all that. All I want is the integers (example, 0, 1, 2, 3, 4, 5 and not 1.001, 2.33 etc.)
Thank you.

I am writing modified this code

/*
  ReadAnalogVoltage
  Center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 Dr. G
*/
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1050.0);
  // print out the value you read:
  Serial.println(voltage);
  delay(200);  // delay in between reads for stability
}

Your topic does not indicate a problem with IDE 2.x and hence has been moved to a more suitable location on the forum.

float pi = 3.14;
int value = (int)pi)

Not tested but should print 3.

Try:

1 Like

Thank you. The plotter reads one fractional value, i.e. 3.5, 3.7, 2.5, 2.2 etc. and not 1, 2 3, 4, 5, etc. but I can live with the single fraction. Also, I noticed the jitter I was getting previously is gone. New breadboard, new pot, new Arduino, less electrical noise (yes, our workshops yet have fluorescent bulbs) .

/*
  ReadAnalogVoltage
  Center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 modified by yours truly Dr. G
*/
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1050.0);
  // print out the value of the potentiometer as an interger only:
Serial.println(voltage,0);
  delay(200);  // delay in between reads for stability
}

Thank you, and appreciate your patience with a newbie.

/*
  ReadAnalogVoltage
  Center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
 modified by yours truly Dr. G
*/
// the setup routine runs once when you press reset:
void setup() {
  // initialize serial communication at 9600 bits per second:
  Serial.begin(9600);
}

// the loop routine runs over and over again forever:
void loop() {
  // read the input on analog pin 0:
  int sensorValue = analogRead(A0);
  // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):
  float voltage = sensorValue * (5.0 / 1050.0);
  // print out the value of the potentiometer as an interger only:

float pi = 3.14;
int value = (int)pi);
Serial.println(voltage,0);

  delay(200);  // delay in between reads for stability
}

C:\Users\Acer_new\Documents\Arduino\AnalogReadSerial_with_pi\AnalogReadSerial_with_pi.ino: In function 'void loop()':
C:\Users\Acer_new\Documents\Arduino\AnalogReadSerial_with_pi\AnalogReadSerial_with_pi.ino:21:20: error: expected ',' or ';' before ')' token
int value = (int)pi);
^

exit status 1

Compilation error: expected ',' or ';' before ')' token

Sorry, typo. That last ) should not be there

1 Like

That was an example how to truncate a float… not something that you would have in your code…

1 Like

Did you intend 1050.0, or 1024.0?

1 Like

look this over

Output

 anlg  729, val   3.47, num   3
 anlg  724, val   3.45, num   3
 anlg  715, val   3.40, num   3
 anlg  691, val   3.29, num   3
 anlg  672, val   3.20, num   3
 anlg  657, val   3.13, num   3
 anlg  642, val   3.06, num   3
 anlg  624, val   2.97, num   3
 anlg  606, val   2.89, num   3
 anlg  584, val   2.78, num   3
 anlg  564, val   2.69, num   3
 anlg  552, val   2.63, num   3
 anlg  529, val   2.52, num   3

 anlg  517, val   2.46, num   2
 anlg  497, val   2.37, num   2
 anlg  485, val   2.31, num   2
 anlg  463, val   2.20, num   2
 anlg  451, val   2.15, num   2
 anlg  434, val   2.07, num   2
 anlg  420, val   2.00, num   2
 anlg  394, val   1.88, num   2
 anlg  389, val   1.85, num   2
 anlg  361, val   1.72, num   2
 anlg  361, val   1.72, num   2
 anlg  331, val   1.58, num   2
 anlg  328, val   1.56, num   2

 anlg  301, val   1.43, num   1
 anlg  280, val   1.33, num   1
 anlg  271, val   1.29, num   1
 anlg  241, val   1.15, num   1
 anlg  240, val   1.14, num   1
 anlg  208, val   0.99, num   1
 anlg  200, val   0.95, num   1
 anlg  176, val   0.84, num   1
 anlg  142, val   0.68, num   1
 anlg  139, val   0.66, num   1
 anlg  106, val   0.50, num   1

 anlg   86, val   0.41, num   0
 anlg   73, val   0.35, num   0
 anlg   42, val   0.20, num   0
 anlg   42, val   0.20, num   0
 anlg   41, val   0.20, num   0
 anlg   41, val   0.20, num   0
char s [90];
char t [20];

void
loop (void)
{
    int   anlg = analogRead (A0);
    float val  = anlg * 5.0 / 1050.0;   // scale
    int   num  = val  + 0.5;            // integerize, round up

    dtostrf (val, 6, 2, t);         // sprintf doesn't support %f
    sprintf (s, " anlg %4d, val %s, num %3d", anlg, t, num);
    Serial.println (s );

    delay (1000);
}

void
setup (void)
{
    Serial.begin (9600);
}
1 Like

With a println(voltage,0), The jitter isn't gone. It is just discarded by the reporting process.

1 Like

Thanks, David. I did 1050.0 while playing with the numbers. I was measuring the actual voltage (with a DMM) and 1050.0 was the most accurate. Yes, 1024.0 is the "official" denominator. My code writing would not pass conscientious and meticulous
examination.

DaveXFaraday, you are good. Thank you.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.