Pls help with my ir counter

//i cant make dCM to start from 0 again after dM is 1

#include <TFT_eSPI.h>
#include <SPI.h>
int irPin = 22; 
int count = 0; 
boolean state = true; 
int dCM;
int dM;
int dKM;
TFT_eSPI tft = TFT_eSPI();
void setup(void) 
    {
    Serial.begin(9600); 
    pinMode(irPin, INPUT);
    tft.init();
    tft.setRotation(2);
    }
void loop() 
    {
    int result = count;
    if (!digitalRead(irPin) && state)
        { 
        count++; 
        state = false;
        if(dCM = result)
        {
        if(dCM > 100)
            {
            //dCM = 0;
            dM = result/100;
            if(dM > 1000)
                {
              //dM = 0;
                dKM = result/100000;
                 }
            }
        }
        String p1="cm "; 
        String p2="m ";
        String p3="Km ";
        Serial.println(dKM + p3 + dM + p2 + dCM + p1);
        tft.fillScreen(TFT_BLACK);
        tft.setCursor(10, 10, 2);
     tft.setTextColor(TFT_GREEN,TFT_BLACK);
        tft.setTextSize(2);
        tft.println(dKM + p3 + dM + p2 + dCM + p1);
        //Serial.print("Count: ");
        // Serial.println(count);
      //  delay(100); 
        }
     if(digitalRead(irPin)) 
        { 
        state = true;
        delay(20);
         } 
    }

Ir_counter_v2_1.ino (2.8 KB)

i don't see any code to reset the count to zero

int count = 0;
    int result = count;
        count++;
        // Serial.println(count);

Your sketch is too big for Uno or Nano. Pin 22... seems to be a popular ESP32 pin. Is that your board?

You might want to look at this How to get the best out of this forum before you proceed any further.

It will tell you, amongst other thing how to post code correctly on this forum.

We only know what you tell us, and without knowing what you have, we don't stand a chance.

Yes is esp32

But what sort of esp32 there are lots of different types?

I have cyd esp32

So.. in my serial terminal, i see: 0Km 1m 124cm or 0Km 2m 224cm
But i cant find how to make: 0Km 1m 24cm

cm = cm-m*100

Do you mean this one?

Still waiting for you to post your code correctly, and without a blank line between each line of code.

Code to fix your question regarding the zeroing of the distance once it reaches over one meter

void zeroCounterWhenNeeded(){
if( dm > 1000) dm = 0;
}

Call this function at the start of the loop function. and get rid of the nonsense in the body of the function that tries to do this. In general use meaningful names for variables, this is much more easy to read and as the code is compiled it has no effect on the speed nor the size of your code.

Is this exactly what you want to pass to the display, or is it just the numbers?
If the former then you will have to assemble the mixture of numbers and ASCII characters into a string. This is a C type string with a lower case "s". Do not use the String function, with the upper case "S" as that can lead to memory problems in the long run.

Looking forward to seeing your code posted correctly.

Yes, this is the display, I rebuilt the sketch differently and succeeded, but the measurement starts like this 0km 0m 1cm and I don't know why

#include <TFT_eSPI.h>
#include <SPI.h>
int irpin = 22; 
int dcm = 0;
boolean state = true;
byte ocm = 99;
byte om = 999;
int dm;
int dkm;
TFT_eSPI tft = TFT_eSPI();
void setup(void) 
    {
    Serial.begin(9600); 
    pinMode(irpin, INPUT);
    tft.init();
    tft.setRotation(2);
    tft.setTextColor(TFT_GREEN,TFT_BLACK);
    tft.setTextSize(2);
    }
void loop(void) 
    {
    if (!digitalRead(irpin) && state)
        {
        dcm++; 
        state = false;
        if(dcm == 100)
            {
            dcm = 0;
            ocm = dm;
            dm++;
            if(dm > 1000)
                {
                dm = 0;
                dkm++;
                 }
            }
        const char p1 ='cm '; 
        const char p2 ='m ';
        const char p3 ='Km ';
        Serial.println(dkm + p3 + dm + p2 + dcm + p1);
        tft.fillScreen(TFT_BLACK);
        tft.setCursor(10, 10, 2);
        tft.println(dkm + p3 + dm + p2 + dcm + p1);
        //Serial.print("Count: ");
        // Serial.println(count);
      //  delay(100); 
        }
     if(digitalRead(irpin)) 
        { 
        state = true;
        delay(20);
         }
    if (om != dm) 
        { 
      om = dm;
        }
    if (ocm != dcm) 
        {
      ocm = dcm;
        }
   }

Thank you for posting the code correctly.

However, I see you have ignored the advice not to use String but to use string.
Also you have decided not to use the zero reset function I gave you.

Also what is the point of doing this:-

In the loop function? It never changes and so would be much better in the setup function.

Maybe some one else is willing to give you information that you will not ignore?

don't you only update the display when the code recognizes the irpin and don't you increment dcm each time

perhaps you should put the display code in a sub-function that can also be called in setup() before anything happens

With Strings the program ran ok, I found the problem, the problem is that the sketch counts both signals from irpin HIGH and LOW

Yes, I modified the sketch as you advised me, but the display does not show as it should and I returned to my initial sketch

So what you should have done is to post this modified code so we could have tested it and looked for any mistakes you or I had made.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.