Turn counter backward counting

hi, i have done coding for turn counter, want to deduct from counts if we turn in opposite direction.
also i want to make project to turn stepper motor on if a mechanical part reach to some sensor, and stop again if that part reach to that sensor again, during this turn counter should show turns.

here is the code:::

#include <wire.h>
#include<liquidcrystal_I2C.h>

int x = 0;
int input = A0;
int state = 0;

liquidcrystal_I2C lcd(0x27, 16, 2);

void setup ()
{
lcd.init();

lcd.init();
lcd.backlight();
lcd.setcursor(0, 1);
lcd.print(" tPI");
lcd.setcursor (0, 1);
lcd.print(x);
lcd.print(" =turn ");
}
void loop()
}
int counter = digitalread(A0);
if (state == 0)
{
switch (counter) {
case 1 : state = 1; lcd.setcursor (0,1); x=x+1; lcd.print(x); break;
case 0 : state = 0;break;
}
}

@ghufi you posted this in the wrong section so I have moved it here. The section you posted is says clearly it is not for posting about your project.

Why are you using an "int" when the only possibilities are 0 and 1? What is connected to A0? What driver and power supply are you using the the stepper motor?

Hello Paul_KD7HB
The function digitalRead() returns an INT either HIGH or LOW.

Have a nice day and enjoy coding in C++.
Дайте миру шанс!

not clear (to me).
sounds like you want the stepper motor to rotate some amount when sensor becomes active and then stop when sensor becomes active again.

  • should be ("{" instead of "}") and closing brace ("}") at end of program
void loop()
{
  • guessing the A0 is the sensor, not the actual count
  • it's "digitalRead" not digitalread
  • without anything to immediately change the state of the sensor input, "x" is likely to rapidly be incremented many time

consider (but there no motor control

const byte SensorPin = A1;      // my hardware

byte sensorLst;
bool state = 0;
int  count = 0;

void loop()
{
    int sensor = digitalRead (SensorPin);

    if (sensorLst != sensor)  {
        sensorLst = sensor;
        delay (10);             // debounce

        if (LOW == sensor)  {   //active
            state = !state;     // toggle
            if (state)
                count++;
            Serial.println (count);
        }
    }
}

void setup ()
{
    Serial.begin (9600);

    pinMode (SensorPin, INPUT_PULLUP);
    sensorLst = digitalRead (SensorPin);
}

That's right! Was getting late. Likely goes into a register, first.
Paul

Hello Paul_KD7HB
Take a view into the Arduino source code:

int digitalRead(uint8_t pin)
{
	uint8_t timer = digitalPinToTimer(pin);
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);

	if (port == NOT_A_PIN) return LOW;

	// If the pin that support PWM output, we need to turn it off
	// before getting a digital reading.
	if (timer != NOT_ON_TIMER) turnOffPWM(timer);

	if (*portInputRegister(port) & bit) return HIGH;
	return LOW;
}

1 Like

@gcjr sounds like you want the stepper motor to rotate some amount when sensor becomes active and then stop when sensor becomes active again.

You understand right. I am not expert in coding.

If you can help to develop my project.

see the code posted and see if it's doing what you expect

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