// I have a gear lever (like sequential shifter) working with a sensor.
// In normal position it reads 420 - 460 values
// when shift up it reads 240 - 260 values
// when shift down it reads 670 - 700 value
// checked the values with serial monitor by analog pin connected to the signal in sensor.
//i need to shift up and down, but i need only one count at a time!
//i used this code form my old project i used for a pushbutton count, ( by changing a little)
// Gear Lever Sensors
int X = A0; // Foward and Backward Gear Lever
int Y = 2; // Led
// Variables
int GearCount = 1;
// Setting Up Inputs and Outputs
void setup()
{
Serial.begin(9600);
pinMode(X, INPUT); //analog input
pinMode(Y, OUTPUT); // led output
}
// Loops for functions
void loop()
{
Serial.println(analogRead());
FBSensor();
SecondG();
}
// Foward and Backward Gear Shift Count
void FBSensor()
{
analogRead(X);
delay(100);
if(X >= 240 && XX <= 260 && GearCount <13)
{
GearCount = GearCount += 1; // Counts from 0 - 13 when shifting and 13 - 0 when shifting backward
}
}
void SecondG()
if (GearCount == 2)
{
digitalWrite(Y, HIGH); // led on when in 2nd gear ,
}
void FBSensor()
{
analogRead(X);
- Sitting there by itself, analogRead(X); does nothing for you.
Sir, Can you tell me what can i do?
You need something like this:
void FBSensor()
{
int getValue = analogRead(X);
if(getValue >= 240 && getValue <= 260 && GearCount <13)
When I shift, it makes more than one count?
How to make one count at a time?
-
First off, this is wrong Serial.println(analogRead()); change to Serial.println(analogRead(X));
-
You are reading X every time that loop(), loops.
As a result, GearCount = GearCount += 1; happens continuously.
Sir, is there a way to count only once at a time?
Should i put the analog read X under setup?
To stop looping
-
First, slow down the reading of X, once every 200ms is more than adequate.
-
Second, When you read X, check to see if it has changed significantly from the last time you read it.
When it has, that’s the time you can do your GearCount adjustments -
Third, this line is not necessary
pinMode(X, INPUT); //analog input
Thank you Sir, appreciate your help and time.
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.