const int downButtonPin = 7;
const int upButtonPin = 6;
int downButtonState = LOW;
int downReading;
int upButtonState = LOW;
int upReading;
int Setpoint;
void setup()
{
Serial.begin( 9600);
pinMode(downButtonPin, INPUT);// assign pins to the buttons
pinMode(upButtonPin, INPUT);// assign pins to the buttons
Serial.print("Setpoint:" );
}
void loop()
{
// Check states of pushbuttons, if pressed change setpoint up or down
upButtonState = digitalRead(upButtonPin);
if (upButtonState == 0)
Setpoint++;
downButtonState = digitalRead(downButtonPin);
if (downButtonState == 0)
Setpoint--;
Serial.println(Setpoint);
delay(100);
}
For now the Setpoint increase or decreasing the number by 1, now I wanna be able to increase/decrease by 0.1.
Dennis81:
For now the Setpoint increase or decreasing the number by 1, now I wanna be able to increase/decrease by 0.1.
Let us review few things which might help to find solution for you:
1. Using paper and pencil, we can usually add 10 and 0.1 to get: 10 + 0.1 = 10.1.
2. What have we done in Step-1? Have we not done the following?
10 ==> 10.0
0.1
Sum: 10.1
we have put .0 to the right side of 10 to get 10.0 so that we can easily place 0.1 at the beneath in correct alignment of fractional digits.
Now to increase the value of the variable Setpoint by 0.1, we can not do Setpoint++ as this operation augments the value by 1; but, we want the augmentation by 0.1.
Therefore, the solution could be:
float Setpoint = 0.0; //float keyword is used when a variable assumes a number with decimal point
float incremetal = 0.1;
Serial.println(Setpoint, 1); //will show 1-digit after the decimal point (0.1, 0.2, 0.3, ....... 1.1, ...
Use the above hints; bring change in you codes; upload and execute; report the result.
Like many pitfalls of the Arduino language, floats are not more precise than long integers, but a hack to support novice programmers with how they already think through a problem. Like many Arduino hacks, everything seems all good, until one codes
if (6.0/3.0 == 2.0) {
and then can't figure out why this returns false. It would be better (in my humble opinion) to understand that the UNO does not have a arithmetic logic unit that can properly handle floats. And then, with this understanding, approach the problem thinking "How do I make 1 = 0.1? " If your range of values falls between -214,748,364.8 and 214,748,364.7, you can use type long, increment by 1 and fix your output with a sprintf() statement. This may also result in code that is lighter and executes faster because it is more native to the hardware.
Dennis81:
What if I want to keep the int Setpoint and I want the increase/ decrease this with 0.1?
int Setpoint = 25;
float counter = 0.1;
if (upButtonState == 0)
Setpoint =+ counter;
or something?
I ty to understand the logic..
+= not =+... Setpoint += counter; is the same as Setpoint = Setpoint + counter;
Also, it's a good practice to use UpperCamelCase for constants and lowerCamelCase for variables.