system
September 24, 2009, 2:55pm
1
I am new at this and I am trying to simply display decimal values at the serial output. I am having problems with the float variable. can someone help??
#define sensorA 3 // Button 1 at pin3
#define sensorB 4 // button 2 at pin4
int s1 = 1;
int s2 = 1;
const int ledPin = 13;
int s1old = 1; // old state of buttom 1
int s2old = 1; // old state of button 2
int count = 0; // this value will be increased/decreased
int x = 12.56; // constant arc length in inch
bool s1active = false;
bool s2active = false;
void setup()
{
pinMode(sensorA,INPUT);
digitalWrite(sensorA,HIGH); //pull up on
pinMode(sensorB,INPUT);
digitalWrite(sensorB,HIGH); // pull up on
Serial.begin(9600);
}
void loop()
{
s1 = digitalRead(sensorA);
s2 = digitalRead(sensorB);
if (s1old == 1 && s1 == 0) s1active = true; // just pressed
else s1active = false;
if (s2old == 1 && s2 == 0) s2active = true; // just pressed
else s2active = false;
if (s1==0 && !s1active && s2active) count += x;
Serial.print(" Cable Used in inches: ");
Serial.println(count, DEC);
{
if ((count,) % 1 == 0) {
digitalWrite(ledPin, 1);
delay(250);
} else {
digitalWrite(ledPin, 0);
delay(250);
}
}
}
system
September 24, 2009, 2:59pm
2
I am having problems with the float variable
Which are...?
You haven't got any floats.
"int x = 12.56;" is invalid.
"float x = 12.56;" isn't.
Please use the "code" (#)button when posting code
system
September 24, 2009, 3:24pm
3
I am still having the float problem. I am not sure how to implement the float variable.
#define sensorA 3 // Button 1 at pin3
#define sensorB 4 // button 2 at pin4
int s1 = 1;
int s2 = 1;
const int ledPin = 13;
int s1old = 1; // old state of buttom 1
int s2old = 1; // old state of button 2
int count = 0; // this value will be increased/decreased
int count1 = 0;
int count2 = 0;
float x = 12.56; // constant arc length in inch
float y = 1.047; // constant arc length in feet
float z = 0.319; // constant arc length in meters
bool s1active = false;
bool s2active = false;
void setup()
{
pinMode(sensorA,INPUT);
digitalWrite(sensorA,HIGH); //pull up on
pinMode(sensorB,INPUT);
digitalWrite(sensorB,HIGH); // pull up on
Serial.begin(9600);
}
void loop()
{
s1 = digitalRead(sensorA);
s2 = digitalRead(sensorB);
if (s1old == 1 && s1 == 0) s1active = true; // just pressed
else s1active = false;
if (s2old == 1 && s2 == 0) s2active = true; // just pressed
else s2active = false;
if (s1==0 && !s1active && s2active) count += x;
Serial.print(" Cable Used in inches: ");
Serial.println((float)count, DEC);
if (s1==0 && !s1active && s2active) count1 += y;
Serial.print(" Cable Used in feet: ");
Serial.println((float)count1, DEC);
if (s1==0 && !s1active && s2active) count2 += z;
Serial.print(" Cable Used in meters: ");
Serial.println((float)count2, DEC);
if (s2==0 && !s2active && s1active)
count -= x, count1 -= y, count2 -= z;
s1old = s1; // remember the last state
s2old = s2; // remember the last state
{
if ((count, count1,count2) % 1 == 0) {
digitalWrite(ledPin, 1);
delay(250);
} else {
digitalWrite(ledPin, 0);
delay(250);
}
}
}
mem
September 24, 2009, 4:37pm
4
try declaring your count variables as float instead of int
system
September 24, 2009, 5:33pm
5
I have tried that but i still get rounded values, i need to get values up to 3 decimal places at least.
mem
September 24, 2009, 5:49pm
6
I presume you got values to two decimal places when your count variables were floats. If so, why did you put that back to ints?
Floats on arduino are accurate to around 7 decimal places but the print routine only shows two.
system
September 25, 2009, 3:21pm
7
I figured out the problem in my code, I had to use float for the count and the x, y, z values. But the reason i wasnt getting any decimal numbers on my output was because I had to remove "DEC" from my println function. now the program is running fine. thank you all