Up&Down button increase by 0.1

Hello,

This is my code:

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.

Who can help me?

What part do you need help with? Show us what you've tried and let's discuss what went wrong.

Do you know what type of variable you will need? Will an int work still? Why or why not?

int Setpoint;

Do you know what int means in this statement ?

Setpoint++;

Do you understand what this does ? Could you write this a different way ?

if (upButtonState == 0)
            Setpoint++ 0.1;

This will make no difference.

When I use this float Setpoint = 0.1; the counter starts with 0.10, but if I pressed the up button it wil count from 0.10 to 1.10, 2.10 etc..

Dennis81:

if (upButtonState == 0)

Setpoint++ 0.1;




This will make no difference.

That's wrong

Setpoint++

Adds 1.

Try:

Setpoint += 0.1

PerryBebbington:
That's wrong

Setpoint++

Adds 1.

Try:

Setpoint += 0.1

yes this works now!!

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.

float Setpoint = 0.0; with Serial.println(Setpoint,2); shows me 1.00, 2.00 etc.

float Setpoint =0.1; with Serial.println(Setpoint,2); shows me 0.10, 0.20, etc

Thank you very much!

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.

PerryBebbington:
That's wrong

Setpoint++

Adds 1.

Try:

Setpoint += 0.1

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

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.

like this way?

Because this is not working..

int Setpoint = 25;
float counter = 0.1;

upButtonState = digitalRead(upButtonPin);
if (upButtonState == 0)
counter++;
Setpoint += counter;

downButtonState = digitalRead(downButtonPin);
if (downButtonState == 0)
counter--;
Setpoint -= counter;

Serial.println(Setpoint,2);

Perehama:
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 does?
Wow.

Dennis81:
like this way?

Because this is not working..

int Setpoint = 25;
float counter = 0.1;

upButtonState = digitalRead(upButtonPin);
if (upButtonState == 0)
counter++;
Setpoint += counter;

downButtonState = digitalRead(downButtonPin);
if (downButtonState == 0)
counter--;
Setpoint -= counter;

Serial.println(Setpoint,2);

You're adding zero to an int.
What do you expect?

Change the type of Setpoint to the same as the increment

you mean like this?

int Setpoint = 25.0;
float counter = 0.1;

No, the SAME type.

In reply #2 I asked whether you knew what the int means in
int Setpoint;But you never answered

So, do you know what it means ?

Dennis81:
What if I want to keep the int Setpoint and I want the increase/ decrease this with 0.1?

You can't.

Answer the question that UKHeliBob asked you. The secret to understanding you problem lies in the answer to that question.

UKHeliBob:
In reply #2 I asked whether you knew what the int means in

int Setpoint;

But you never answered

So, do you know what it means ?

Now I do after is search it. It only can be 1 or 25...no dec's..