calculate speed using circumference

i want to calculate speed and i used some formulae to calculate it ,i am using to buttons to change circumference but i get speed and frequency and rpm serial monitor circumference don't change anyone help me why this happening i give my code below and i am giving 1hz frequency to arduino

#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 10, 5, 4, 3, 2);
double freq=0;          
unsigned long Htime=0;    //high pulse time;
unsigned long Ltime=0;    //low pulse time;
unsigned int A=8;
unsigned long R=0;   //rpm
float  C=0;         // C as a cicrcumferenece;
float Speed=0;
unsigned long Ttime=0;  //total time;
const int incrementButton = 11;
const int decrementButton = 12;
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 lastIncrementState = 0;
float lastDecrementState = 0;

void setup()
{   
  lcd.begin(16, 2);   
  Serial.begin(9600);
     pinMode(A,INPUT);
   pinMode(incrementButton, INPUT_PULLUP);    //set incrementButton as INPUT;
   pinMode(decrementButton, INPUT_PULLUP);   //set decrementButton as INPUT;
     
  
}//end of setup


void loop()
{    

   incrementState = digitalRead(incrementButton);   //read the increment button state;
  
   if(incrementState != lastIncrementState)   //compare increment button state to its last state;
   {
      if(incrementState == HIGH)    //increment button is pressed;
      {
          Serial.print("C=");
  
         C+=0.1;
         
         Serial.println(C);
              
       delay(20);  }    
   lastIncrementState = incrementState;}
   decrementState = digitalRead(decrementButton);   //read the decrement button state;
   if(decrementState != lastDecrementState)   //compare decrement state to its lastState;
   {
      if(decrementState == HIGH )      //decrement button is pressed;
      {
      Serial.print("C=");
         C-=0.1;              //decrement the counter;
         Serial.println(C);    //print it on serial monitor;
         lcd.setCursor(0, 0);   // top left;
             lcd.print("C=");

             lcd.print(C);
               delay(20);   }  //debounce delay;
     
 
   lastDecrementState = decrementState;
   }
   

    Htime=pulseIn(8,HIGH);           //count high pulses;
  Ltime=pulseIn(8,LOW);            //count low pulse;
  
  Ttime=Htime+Ltime;  //total time calclated

   Serial.println(freq);
  freq=1000000/Ttime;           //frequency calculated 
   Serial.print("frequency=");
 lcd.setCursor(8, 0); // top right
  lcd.print("R=");

  R=freq*60;
  Serial.println(R);
  lcd.print(R);
  
  Serial.print("Speed=");
 lcd.setCursor(0, 1);    // bottom left
  lcd.print("Speed=");
  Speed=C*R*0.06;
  Serial.print(Speed);
  lcd.print(Speed);
   delay(500);
  }

You are using the built in input pull-ups for pins 11 and 12 so button pressed should be comparing with LOW not HIGH; can you explain how you wired your buttons?

but if we separate out the code before htime there was no problem to change value please se this code was running properly and I used 10k resistors to 11th and 12 th pin and gives those to vcc
please check this part of code running properly

unsigned long Ttime=0;  //total time;
const int incrementButton = 11;
const int decrementButton = 12;
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 lastIncrementState = 0;
float lastDecrementState = 0;

void setup()
{   
  lcd.begin(16, 2);   
  Serial.begin(9600);
     pinMode(A,INPUT);
   pinMode(incrementButton, INPUT_PULLUP);    //set incrementButton as INPUT;
   pinMode(decrementButton, INPUT_PULLUP);   //set decrementButton as INPUT;
     
  
}//end of setup


void loop()
{    

   incrementState = digitalRead(incrementButton);   //read the increment button state;
  
   if(incrementState != lastIncrementState)   //compare increment button state to its last state;
   {
      if(incrementState == HIGH)    //increment button is pressed;
      {
          Serial.print("C=");
  
         C+=0.1;
         
         Serial.println(C);
              
       delay(20);  }    
   lastIncrementState = incrementState;}
   decrementState = digitalRead(decrementButton);   //read the decrement button state;
   if(decrementState != lastDecrementState)   //compare decrement state to its lastState;
   {
      if(decrementState == HIGH )      //decrement button is pressed;
      {
      Serial.print("C=");
         C-=0.1;              //decrement the counter;
         Serial.println(C);    //print it on serial monitor;
         lcd.setCursor(0, 0);   // top left;
             lcd.print("C=");

             lcd.print(C);
               delay(20);   }  //debounce delay;
     
 
   lastDecrementState = decrementState;
   }

What I'm saying is

  if(incrementState == HIGH)    //increment button is pressed;[/code does not check if the button is pressed, it checks when it is not.

If you were to wire to ground with the input pull-up as you do in the setup.

Wiring the Pin that you set as INPUT_PULLUP through another resistor to Vcc is not a great idea.... it's basically high on both sides of your button

see this post for this image

yes but if we separate out the increment and decrement code then why its runing properly?i don't what is the problem if we say LOW on both side its also run,please can explain what is hapeening?

and when we add this part in code after circumference the code will not run properly if press key its only display frequency if we press button 5-6 times it will then increment if we escape this part its will incment and decrement properly with button can you explain why this happening see the below part of code and my main code and sorry for my poor english

  Htime=pulseIn(8,HIGH);           //count high pulses;
  Ltime=pulseIn(8,LOW);            //count low pulse;
  
  Ttime=Htime+Ltime;  //total time calclated

   Serial.println(freq);
  freq=1000000/Ttime;           //frequency calculated 
   Serial.print("frequency=");
 lcd.setCursor(8, 0); // top right
  lcd.print("R=");

  R=freq*60;
  Serial.println(R);
  lcd.print(R);
  
  Serial.print("Speed=");
 lcd.setCursor(0, 1);    // bottom left
  lcd.print("Speed=");
  Speed=C*R*0.06;
  Serial.print(Speed);
  lcd.print(Speed);
   delay(500);
  }

yes but if we separate out the increment and decrement code then why its runing properly

I don't know what you mean.... show FULL code working properly and explain how things are wired. Your 2 buttons are NOT correctly wired so they will do garbage. You can't trust anything connected to those buttons that way. I told you twice already so Start fixing this. then we can discuss the rest. I'm not interested in software guessworks on something that is ill designed from the hardware side.

while you are at fixing this, why do you think those should be float type?

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 lastIncrementState = 0;
float lastDecrementState = 0;

Where have you seen that digitalRead() returns a float? why initializing them with 0 instead of LOW which would be conceptually more relevant? (the compiler will initialize them to 0 for you as global variables anyway if you don't do anything)

when you do this

  Htime = pulseIn(8, HIGH);        //count high pulses;
  Ltime = pulseIn(8, LOW);         //count low pulse

you don't know that status of pin 8 so it might wait for pin to go LOW first and then wait for HIGH and count until it goes LOW again. then when you call the second line, as it is LOW, you are waiting for it to go HIGH and then LOW to start counting. So you are not capturing two consecutive HIGH/LOW but there is one apart. not sure if that matters for your stuff

pulse in function counts the pulses i just used low pulse and high pulse to calculate time period

please tell me where i was wrong in my code i don't get any idea and what are telling can you explain me in some diffrent way?

that's what I mean you are measuring:


so you are not measuring consecutive HIGH LOW - just in case that matters.

Amol777:
please tell me where i was wrong in my code i don't get any idea and what are telling can you explain me in some diffrent way?

PLEASE SHOW ME THE CODE THAT IS SUPPOSEDLY WORKING
PLEASE FIX THE BUTTONS WIRING. THIS IS WRONG.

if you don't then I hope you'll find some charitable soul to help out. I'm done. can't fix code with bad hardware wiring. it's totally useless. I'll use my time in a better way.

total time period of pulse then i will used it to calculate frequency and your showing write in digram i am taking Ltime+Htime =Total time

Hi,
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:

see the attachment i just draw a digram

with the builtin pull-ups you don't need the external ones

but if i don't use that pull ups the button increment with 2 it takes 2 pulses when i will press button its increment by two and like 11,12

what if you wire as in the above drawing (yours modified without the external resistors) and use that code
(set your Serial monitor at 115200, no need to run at 9600 bauds for the communication with your PC)
I've not verified your maths

#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 10, 5, 4, 3, 2);

const int incrementButton = 11;
const int decrementButton = 12;
const byte pulsePin = 8;

byte incrementState;         //variable that will read the increment button (either HIGH or LOW);
byte decrementState;        //variable that will read the decrement button (either HIGH or LOW);
byte lastIncrementState = LOW;
byte lastDecrementState = LOW;

double freq = 0;
unsigned long Htime = 0; //high pulse time;
unsigned long Ltime = 0; //low pulse time;
unsigned long Ttime = 0; //total time;

unsigned long R = 0; //rpm
double  C = 0;       // C as a cicrcumferenece;
double Speed = 0;


void setup()
{
  lcd.begin(16, 2);
  Serial.begin(115200);
  pinMode(pulsePin, INPUT);
  pinMode(incrementButton, INPUT_PULLUP);    //set incrementButton as INPUT;
  pinMode(decrementButton, INPUT_PULLUP);   //set decrementButton as INPUT;
}//end of setup


void loop()
{
  static unsigned long t0 = millis();

  incrementState = digitalRead(incrementButton);   //read the increment button state;
  if ((incrementState != lastIncrementState) && (incrementState == LOW)) {
    //increment button is pressed;
    C += 0.1;
    lcd.setCursor(0, 0);   // top left;
    lcd.print("C="); lcd.print(C);
    delay(20);  //debounce delay;
  }
  lastIncrementState = incrementState;

  decrementState = digitalRead(decrementButton);   //read the decrement button state;
  if ((decrementState != lastDecrementState) && (decrementState == LOW )) {
    //decrement button is pressed;
    C -= 0.1;            //decrement the counter;
    Serial.print("C="); Serial.println(C);    //print it on serial monitor;
    lcd.setCursor(0, 0);   // top left;
    lcd.print("C="); lcd.print(C);
    delay(20);  //debounce delay;
  }
  lastDecrementState = decrementState;

  if (millis() - t0 > 1000) { // udpate display once per second
    Htime = pulseIn(pulsePin, HIGH);        //count high pulses;
    Ltime = pulseIn(pulsePin, LOW);         //count low pulse;
    Ttime = Htime + Ltime; //total time calclated

    freq = ((double) 1000000.0) / Ttime;       //frequency calculated
    Serial.print("frequency=");  Serial.println(freq);

    R = freq * 60ul;
    Serial.print("R=");  Serial.println(R);
    lcd.setCursor(8, 0); // top right
    lcd.print("R=");  lcd.print(R);

    Speed = C * R * 0.06;
    Serial.print("Speed=");  Serial.print(Speed);
    lcd.setCursor(0, 1);    // bottom left
    lcd.print("Speed=");  lcd.print(Speed);

    t0 +=1000;
  }

}

i will check it and give you reply thanku reply thanku

to increment c its takes more than 6 button press J-M-L-N not wrorking this code thanku for help
:-\

change

byte lastIncrementState = HIGH;
byte lastDecrementState = HIGH;

in the setup

have you removed your external resistor? are you wiring to the arduino GND through the buttons? are you using the right pins of your butons?