about increment float value

I want to read float value to increment value in decimal point values like 1.1,1.2,1.3,1.4 by using button

float f = 1;
void setup()
{
  ...
  ...
}

void loop()
{
  if(button is pressed)
    f+= 0.1;
}

Something like the above 'pseudo' code?

Be aware of bouncing so a single press might result in a number of increments. You might also want to react only on a change from not pressed to pressed (see e.g. the state-change example that comes with the IDE).

I Suggest increment an integer
11,12,13,14...
then multiply it by 0.1 to make it a float when you need it

void setup() {
  Serial.begin(115200); //115200

}

void loop() {
  static int Counter;
  Counter++;
  Serial.println((float) Counter * 0.1, 1);
  delay(100);
}

Z

I will check update my ans thanku for suggestion and reply

i used below code to increment and decrement anyone tell me what is wrong in that code don't get any output i tried to find error and code also compiling in software sorry for my poor English

const int incrementButton = 7;
const int decrementButton = 9;

//Global variables
float incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
float decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
float counter = 0; //variable that will store the count
float lastIncrementState = 0;
float lastDecrementState = 0;



void setup()
{    Serial.begin(9600);
   pinMode(incrementButton, INPUT); //set incrementButton as INPUT
   pinMode(decrementButton, INPUT); //set decrementButton as INPUT
   Serial.print("counter");
   
}//end of setup

void loop()
{

   incrementState = digitalRead(incrementButton); //read the increment button state
   decrementState = digitalRead(decrementButton); //read the decrement button state
   if(incrementState != lastIncrementState) //compare increment button state to its last state
   {
      if(incrementState == HIGH&& counter <0)//increment button is pressed
      {
         counter = counter+=0.1;
         
         Serial.println(counter);
        } //debounce delay  delay(20);
     
   }
   lastIncrementState = incrementState;

   if(decrementState != lastDecrementState)//compare decrement state to its lastState
   {
      if(decrementState == HIGH && counter >0)//decrement button is pressed
      {
         counter = counter--; //decrement the counter
         Serial.println(counter); //print it on serial monitor
         
         } //debounce delay delay(20);
     
   }
   lastDecrementState = decrementState;
}//end of loop
[code]
counter = counter+=0.1;

Why ? Either use counter += 0.1; or counter = counter + 0.1;.

Also note that you don't have a debounce delay; maybe intentionally?

i have changed code and then run it but not working i have used your logic and i added debounce delay

const int incrementButton = 7;
const int decrementButton = 9;

//Global variables
float incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
float decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
float counter = 0; //variable that will store the count
float lastIncrementState = 0;
float lastDecrementState = 0;



void setup()
{    Serial.begin(9600);
   pinMode(incrementButton, INPUT); //set incrementButton as INPUT
   pinMode(decrementButton, INPUT); //set decrementButton as INPUT
   Serial.print("counter");
   
}//end of setup

void loop()
{

   incrementState = digitalRead(incrementButton); //read the increment button state
   decrementState = digitalRead(decrementButton); //read the decrement button state
   if(incrementState != lastIncrementState) //compare increment button state to its last state
   {
      if(incrementState == HIGH&& counter <0)//increment button is pressed
      {
         counter = counter += 0.1;
         
         Serial.println(counter);
     delay(20);   } //debounce delay  delay(20);
     
   }
   lastIncrementState = incrementState;

   if(decrementState != lastDecrementState)//compare decrement state to its lastState
   {
      if(decrementState == HIGH && counter >0)//decrement button is pressed
      {
         counter = counter--; //decrement the counter
         Serial.println(counter); //print it on serial monitor
         
       delay(20);  } //debounce delay delay(20);
     
   }
   lastDecrementState = decrementState;
}//end of loop

Ok, delay done. But you did not fix the other issue that I mentioned.

i don't understand can you explain it

float counter = 0; //variable that will store the count
if(incrementState == HIGH&& counter <0)
if(decrementState == HIGH && counter >0)

With counter ==0, you will never enter either of these conditons.

Since you have two buttons, increment and decrement, I don't think you even need the <0 or >0 condition to allow them to be active.

Change

counter = counter += 0.1;

To

counter += 0.1;

i did the changes but code still not working, increment done and decrement not done ,can you explain why this code is not working and sorry for my poor english

const int incrementButton = 7;
const int decrementButton = 9;

//Global variables
float incrementState = 0; //variable that will read the increment button (either HIGH or LOW)
float decrementState = 0; //variable that will read the decrement button (either HIGH or LOW)
float counter = 0; //variable that will store the count
float lastIncrementState = 0;
float lastDecrementState = 0;



void setup()
{    Serial.begin(9600);
   pinMode(incrementButton, INPUT); //set incrementButton as INPUT
   pinMode(decrementButton, INPUT); //set decrementButton as INPUT
   Serial.print("counter");
   
}//end of setup

void loop()
{

   incrementState = digitalRead(incrementButton); //read the increment button state
   decrementState = digitalRead(decrementButton); //read the decrement button state
   if(incrementState != lastIncrementState) //compare increment button state to its last state
   {
      if(incrementState == HIGH)//increment button is pressed
      {
         counter += 0.1;
         
         Serial.println(counter);
     delay(20);   } //debounce delay  delay(20);
     
   }
   lastIncrementState = incrementState;

   if(decrementState != lastDecrementState)//compare decrement state to its lastState
   {
      if(decrementState == HIGH )//decrement button is pressed
      {
         counter-=0.1; //decrement the counter
         Serial.println(counter); //print it on serial monitor
         
       delay(20);  } //debounce delay delay(20);
     
   }
   lastDecrementState = decrementState;
}//end of loop

Hi,
How have you got your up and down buttons wired.

If you have the digital input wired so the button switches it to 5V, then you need a 10K resistor connected to each digital input and gnd.

This is to ensure the digital input goes LOW when the button is not pulling the digital input HIGH.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom.. :slight_smile:

this code for increment it is working properly

void setup()
{    Serial.begin(9600);
   pinMode(incrementButton, INPUT); //set incrementButton as INPUT
   pinMode(decrementButton, INPUT); //set decrementButton as INPUT
   Serial.print("counter");
   
}//end of setup

void loop()
{

   incrementState = digitalRead(incrementButton); //read the increment button state
   decrementState = digitalRead(decrementButton); //read the decrement button state
   if(incrementState != lastIncrementState) //compare increment button state to its last state
   {
      if(incrementState == HIGH)//increment button is pressed
      {
         counter+= 0.1;
         
         Serial.println(counter);
     delay(20);   } //debounce delay  delay(20);
     
   }
   lastIncrementState = incrementState;

}

and second for decrement this is also working properly

void setup()
{    Serial.begin(9600);
   pinMode(incrementButton, INPUT); //set incrementButton as INPUT
   pinMode(decrementButton, INPUT); //set decrementButton as INPUT
   Serial.print("counter");
   
}//end of setup

void loop()
{

   incrementState = digitalRead(incrementButton); //read the increment button state
   decrementState = digitalRead(decrementButton); //read the decrement button state
   if(incrementState != lastIncrementState) //compare increment button state to its last state
   {
      if(incrementState == HIGH)//increment button is pressed
      {
         counter-= 0.1;
         
         Serial.println(counter);
     delay(20);   } //debounce delay  delay(20);
     
   }
   lastIncrementState = incrementState;

}

if I combine both the code in one one single program as you see in above reply its not working its giving either incrementing value or decrementing at time please tell me how to use increment and decrement in single code and there is no issues in hardware

First here are few questions,
Have you connected you second terminal of switch to vcc or ground?( If Its working I guess its to VCC as you haven't pulled it up!
why you making simple code so complex with finding the previous state and current state if you just want to do it with push switch or do you have any circuit which going to change input of pins? so you want to check those change overs or you want to just increment or decrements the counter with button press?
If just increment or decrement then Is there any problem making it blocking? Means why dont you just use while and keep controller there until key is pressed?

yatin:
why you making simple code so complex with finding the previous state and current state if you just want to do it with push switch

State change detection?

why not just wait till button pressed so automatically state will be regained once switch is released like this?

if(digitalRead(Switch)==LOW){
counter+=0.1;
while(digitalRead(Switch)==LOW);
delay(20);///debounce
}

There is nothing wrong with the code you posted in Reply #11. It is working as written with my buttons.

There is definitely a problem with your buttons or their wiring. Can you show a photograph of what you have done? A hand drawn schematic?

I always configure my buttons as INPUT_PULLUP with one wire to the arduino input and the other to ground. I wire diagonally across the button to ensure that I have picked up two switched legs.

thanku @cattledog now my code is working on hardware thanku for suggetion

thanku yatin sir I got new simple code from your logic see the new code given below its simple and few line code only

#define Switch 10
#define Switch2 8

float Counter=0;

void setup() {

  // put your setup code here, to run once:

  Serial.begin(9600);

  Serial.println("Starting...");

pinMode(Switch,INPUT_PULLUP);
pinMode(Switch2,INPUT_PULLUP);

}




void loop() {

  // put your main code here, to run repeatedly:

if(digitalRead(Switch)==0){

  Counter+=0.1;

  Serial.print("Counter=");

  Serial.println(Counter); 

  while(digitalRead(Switch)==0);

  delay(20);//Debounce 

}

if(digitalRead(Switch2)==0){

  Counter-=0.1;

  Serial.print("Counter=");

  Serial.println(Counter); 

  while(digitalRead(Switch2)==0);

  delay(20);//Debounce 

}

}