please help

Hello,
Just wondering if anyone might have the patience to help me with my novice code
I am reading from a 12bit ADC which is connected to a 4D systems display but because the 4D systems display doesnt support floating point I have decided to send to an Arduino, do the calculation and send back again (this is a one off project)
I know it sounds a round about way but done alot of coding with the 4d systems and hav'nt the time to change everything to Arduino
Everything reads back to the display i.e 0-15 for the ADC1 and 0-255 for ADC2 and if I comment out the calculations and un-comment the print 2.77 I get 2.77 back to the 4d systems, but in the following code I just get 0
As you can gather I am new to C++ and have tried to include everything under the sun, but I must be making a 'school boy' error here
PS. 4d systems is working fine

Would like a bit of help if possible
Cheers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <stdio.h>      /* printf */
#include <stdlib.h>     /* abs */
#include <math.h>
int adc1,adc2;

void setup() {
 
 
  Serial1.begin(9600);

}

void loop() {
  
  
  if (Serial1.available()>=0) {
    adc1 = Serial1.read();
    delay (10);
    adc2 = Serial1.read();
    delay (10);
  adc1 = (adc1*256)+adc2;
  adc1 = (adc1/4095)*5;

    Serial1.print(adc1);
 delay (10);
// Serial1.print (2.77);
// delay (10);
  }
}
adc1 = (adc1/4095)*5;

adc1 and 4095 are both integers.
When you divide two integers... the result will be an integer. IE: 1/4 is 0... not 0.25.


Solutions:

1) Do the multiplication first to prevent data from being truncated
2) Do floating point division by making either adc1 or 4095 a floating point. IE:

adc1 = (adc1 / 4095.0)*5; // <- 4095.0 makes it a floating point
Thanks Disch..You are star! This has been doing my head in..
Topic archived. No new replies allowed.