int FWGear = 3; //Foward button
int BWGear = 4; //Backward button
int NGear = 5; //Netural button
int RGear = 6; //Reverse button
//variables
int GearCount = 1;
int FWGearNew;
int FWGearOld = 1;
int BWGearNew;
int BWGearOld = 1;
int NGearNew;
int NGearOld = 1;
int RGearNew;
int RGearOld = 1;
// led pins
int NLed = 7;
int G1Led = 8;
int G2Led = 9;
int G3Led = 10;
int G4Led = 11;
int G5Led = 12;
int G6Led = 14;
int G7Led = 15;
int G8Led = 16;
void setup ()
{
pinMode(FWGear, INPUT_PULLUP); //Foward button
pinMode(BWGear, INPUT_PULLUP); //Backward button
pinMode(NGear, INPUT_PULLUP); //Netural button
pinMode(RGear, INPUT_PULLUP); //Reverse button
pinMode(NLed, OUTPUT);
pinMode(G1Led, OUTPUT);
pinMode(G2Led, OUTPUT);
pinMode(G3Led, OUTPUT);
pinMode(G4Led, OUTPUT);
pinMode(G5Led, OUTPUT);
pinMode(G6Led, OUTPUT);
pinMode(G7Led, OUTPUT);
pinMode(G8Led, OUTPUT);
}
void loop()
{
FWbutton();
BWbutton();
Nbutton();
Rbutton();
Reverse();
Neutral();
First();
Second();
Third();
Forth();
Fifth();
Sixth();
Seventh();
}
// Foward Gear Button Count
void FWbutton()
{
FWGearNew=digitalRead(FWGear);
delay(100);
if(FWGearOld==1 && FWGearNew==0 && GearCount <8)
{
GearCount = GearCount += 1;
}
FWGearOld=FWGearNew;
}
// Backward Gear Button Count
void BWbutton()
{
BWGearNew=digitalRead(BWGear);
delay(100);
if(BWGearOld==1 && BWGearNew==0 && GearCount >1)
{
GearCount = GearCount -= 1;
}
BWGearOld=BWGearNew;
}
void Nbutton()
{
NGearNew=digitalRead(NGear);
delay(100);
if(NGearOld==1 && NGearNew==0 && GearCount >-1)
{
GearCount = 1;
}
NGearOld=NGearNew;
}
void Rbutton()
{
RGearNew=digitalRead(RGear);
delay(100);
if(RGearOld==1 && RGearNew==0 && GearCount >0)
{
GearCount = 0;
}
RGearOld=RGearNew;
}
// Led Funtions Based On Button Count
**void Reverse()**
**{**
** if(GearCount==0)**
** {**
** digitalWrite(NLed, HIGH);**
** }**
** else**
** {**
** digitalWrite(NLed, LOW);**
** }**
**}**
**void Neutral()**
**{**
** if(GearCount==1)**
** {**
** digitalWrite(G1Led, HIGH);**
** }**
** else**
** {**
** digitalWrite(G1Led, LOW);**
** }**
**}**
void First()
{
if(GearCount==2 )
{
digitalWrite(G2Led, HIGH);
delay(500);
digitalWrite(G2Led, LOW);
}
else
{
digitalWrite(G2Led, LOW);
}
}
void Second()
{
if(GearCount==3)
{
digitalWrite(G3Led, HIGH);
}
else
{
digitalWrite(G3Led, LOW);
}
}
void Third()
{
if(GearCount==4)
{
digitalWrite(G4Led, HIGH);
}
else
{
digitalWrite(G4Led, LOW);
}
}
void Forth()
{
if(GearCount==5)
{
digitalWrite(G5Led, HIGH);
}
else
{
digitalWrite(G5Led, LOW);
}
}
void Fifth()
{
if(GearCount==6)
{
digitalWrite(G6Led, HIGH);
}
else
{
digitalWrite(G6Led, LOW);
}
}
void Sixth()
{
if(GearCount==7)
{
digitalWrite(G7Led, HIGH);
}
else
{
digitalWrite(G7Led, LOW);
}
}
void Seventh()
{
if(GearCount==8)
{
digitalWrite(G8Led, HIGH);
}
else
{
digitalWrite(G8Led, LOW);
}
}
Hello too thanks for saying hello to everyone
A lot of people want some stuff here. But also most of them provide more details and ask kindly for a support
can you help me with this
-
Tell us about your experience in hardware and software ?
-
Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
Give links to components. -
Show us your code preamble laying out the design ?
-
What are all the ************* about ?
2 buttons to shift up and down from zero to seven
counts from 0 - 7 up and down with buttons
for button counts I have set leds
when button count is 1, its given led starts to blink continuously, I need only to blink once
- Always show us a good schematic of your proposed circuit.
Show us good images of your ‘actual’ wiring.
- What are the colours on the LED series resistors ? O/O/BK
Orange Orange Brown Gold,
you can see the led is one,
i need it to turn off after 500 mili secs
- When exactly is the LED(s) supposed to come ON ?
Available from the IDE library manager.
Try this:
void First()
{
static bool blinked = false;
if(GearCount==2)
{
if (!blinked)
{
digitalWrite(G2Led, HIGH);
delay(500);
digitalWrite(G2Led, LOW);
blinked = true;
}
}
else
{
digitalWrite(G2Led, LOW);
blinked = false;
}
}
the program is for a gear box.
total 4 buttons
9 gears , reverse , netrual and 1 - 7
two buttons to shift up and down
one for netrual gear
one for reverse gear
just like sequential shifter
gears are indicated buy leds..
there is a solanice for each gear in gear box,
i m going to give led wire line to turn on the solanides
so i dont need led wire line to continuesly turn on,
only 500 mili sec power is enough to turn on the solanide.
so when i shift a gear, led for that gear is turning on,
but i need to stop it after 500 mili sec
- Tell us what you think this does ?
GearCount = GearCount += 1;
example
if gear count is 1, when i press the FWGear button (Shift up) gear count will be 2
adding one by one
GearCount = GearCount += 1;
Maybe you meant:
GearCount = GearCount + 1;
Thank mate,
This code worked................
yes
-
Had some spare time, try this version.
-
See if you can prevent going to Reverse if you are in gear ?
//
//================================================^================================================
// B a s i c S k e l e t o n S k e t c h
//
// URL
//
//
//
// Version YY/MM/DD Comments
// ======= ======== ========================================================================
// 1.00 23/01/14 Running code
//
//
//
//
//
// Notes:
//
//
//
//
//================================================
#define LEDon HIGH //PIN---[220R]---A[LED]K---GND
#define LEDoff LOW
#define PRESSED LOW //+5V---[Internal 50k]---PIN---[Switch]---GND
#define RELEASED HIGH
#define CLOSED LOW //+5V---[Internal 50k]---PIN---[Switch]---GND
#define OPENED HIGH
#define ENABLED true
#define DISABLED false
#define MAXIMUM 180
#define MINIMUM 0
#define RELAYon LOW
#define RELAYoff HIGH
//================================================
#define ONE_SECOND 1000ul //milliseconds
#define ONE_MINUTE (ONE_SECOND * 60ul) //60 seconds in one minute
#define ONE_HOUR (ONE_MINUTE * 60ul) //60 minutes in one hour
#define ONE_DAY (ONE_HOUR * 24ul) //24 hours in one day
// c l a s s m a k e T I M E R
//================================================^================================================
//
/*
//========================
makeTIMER toggleLED =
{
//.TimerType, .Interval, .TimerFlag, .Restart, .SpeedAdjustPin
MILLIS/MICROS, 500ul, ENABLED/DISABLED, YES/NO, A0-A5
//.SpeedAdjustPin defaults to 0 i.e. no speed adjustment is used
//if .SpeedAdjustPin = A0-A5, a potentiometer on this pin adjusts the TIMER's speed (for diagnostics)
//class static flag "makeTIMER::normalFlag" can be used to ENABLE/DISABLE adjustable TIMER speed,
//ENABLE = normal speed, DISABLED = potentiometer controls TIMER speed
};
TIMER functions we can access:
toggleLED.checkTIMER();
toggleLED.enableRestartTIMER();
toggleLED.restartTIMER()
toggleLED.disableTIMER();
toggleLED.expireTimer();
toggleLED.setInterval(100ul);
Static variable access
makeTIMER::normalFlag = ENABLED/DISABLED //defaults to DISABLED at power up time i.e. variable speed is allowed
*/
// millis() / micros() B a s e d T I M E R S
//================================================^================================================
//
//These TIMER objects are non-blocking
class makeTIMER
{
#define MILLIS 0
#define MICROS 1
#define ENABLED true
#define DISABLED false
#define YES true
#define NO false
#define STILLtiming 0
#define EXPIRED 1
#define TIMERdisabled 2
private:
public:
static bool s_normalFlag; //when ENABLED, adjustable TIMERs run at normal speed
unsigned long Time; //when the TIMER started
//these "members" are needed to define a TIMER
byte TimerType; //what kind of TIMER is this? MILLIS/MICROS
unsigned long Interval; //delay time which we are looking for
bool TimerFlag; //is the TIMER enabled ? ENABLED/DISABLED
bool Restart; //do we restart this TIMER ? YES/NO
byte SpeedAdjustPin; //a potentiometer on this pin, A0-A5, adjusts TIMER speed
//================================================
//constructor with no parameters
makeTIMER()
{
TimerType = MILLIS;
Interval = 1000ul;
TimerFlag = ENABLED;
Restart = YES;
SpeedAdjustPin = 0;
Time = 0;
}
//================================================
//constructor with parameters
makeTIMER(byte _TimerType, unsigned long _Interval,
bool _TimerFlag, bool _Restart, byte _SpeedAdjustPin = 0)
{
TimerType = _TimerType;
Interval = _Interval;
TimerFlag = _TimerFlag;
Restart = _Restart;
SpeedAdjustPin = _SpeedAdjustPin;
Time = 0;
}
//================================================
//condition returned: STILLtiming (0), EXPIRED (1) or TIMERdisabled (2)
//function to check the state of our TIMER ex: if(myTimer.checkTIMER() == EXPIRED);
byte checkTIMER()
{
//========================
//is this TIMER enabled ?
if (TimerFlag == ENABLED)
{
//============
//is this an adjustable TIMER OR is the "normalSpeed" switch closed ?
if (SpeedAdjustPin == 0 || s_normalFlag == ENABLED)
{
//============
//this TIMER "is not" speed adjustable,
//has this TIMER expired ?
if (getTime() - Time >= Interval)
{
//============
//should this TIMER restart again?
if (Restart == YES)
{
//restart this TIMER
Time = getTime();
}
//this TIMER has expired
return EXPIRED;
}
}
//============
//this TIMER is speed adjustable
else
{
//============
//for diagnostics, we use a potentiometer to adjust TIMER speed,
//has this TIMER expired ?
if (getTime() - Time >= Interval / adjustInterval())
{
//============
//should this TIMER restart again?
if (Restart == YES)
{
//restart this TIMER
Time = getTime();
}
//this TIMER has expired
return EXPIRED;
}
}
return STILLtiming;
} //END of if (TimerFlag == ENABLED)
//========================
else
{
//this TIMER is disabled
return TIMERdisabled;
}
} //END of checkTime()
//================================================
//function to enable and restart this TIMER ex: myTimer.enableRestartTIMER();
void enableRestartTIMER()
{
TimerFlag = ENABLED;
//restart this TIMER
Time = getTime();
} //END of enableRestartTIMER()
//================================================
//function to disable this TIMER ex: myTimer.disableTIMER();
void disableTIMER()
{
TimerFlag = DISABLED;
} //END of disableTIMER()
//================================================
//function to restart this TIMER ex: myTimer.restartTIMER();
void restartTIMER()
{
Time = getTime();
} //END of restartTIMER()
//================================================
//function to force this TIMER to expire ex: myTimer.expireTimer();
void expireTimer()
{
//force this TIMER to expire
Time = getTime() - Interval;
} //END of expireTimer()
//================================================
//function to set the Interval for this TIMER ex: myTimer.setInterval(100);
void setInterval(unsigned long value)
{
//set the Interval
Interval = value;
} //END of setInterval()
//================================================
//function to return the current time
unsigned long getTime()
{
//return the time i.e. millis() or micros()
//========================
if (TimerType == MILLIS)
{
return millis();
}
//========================
else
{
return micros();
}
} //END of getTime()
//================================================
//for diagnostics, a potentiometer on an analog pin is used to adjust TIMER speed, thanks alto777
unsigned int adjustInterval()
{
unsigned int Speed = analogRead(SpeedAdjustPin);
//using integer math to save on memory
Speed = 1 + (Speed * 14) / 1023; //Speed will have a range from 1 to 15
return Speed;
} //END of adjustInterval()
}; //END of class makeTIMER
//================================================
//initialize the static "s_normalFlag" variable,
//when ENABLED, adjustable TIMERs run at normal speed
bool makeTIMER::s_normalFlag = DISABLED;
//================================================^================================================
// T I M E R D e f i n i t i o n s
//================================================^================================================
//
//========================
//example: uses default library values
//.TimerType, .Interval, .TimerFlag, .Restart, .SpeedAdjustPin
// MILLIS, 1000ul, ENABLED, YES, 0
//makeTIMER testTIMER{};
//========================
makeTIMER heartbeatTIMER =
{
//.TimerType, .Interval, .TimerFlag, .Restart, .SpeedAdjustPin (A5 is the potentiometer pin)
MILLIS, 500ul, ENABLED, YES, 0
};
//======================== (5ms * s_filter) i.e. 5ms * 10 = 50ms for checking a valid switch operation
makeTIMER switchesTIMER =
{
//.TimerType, .Interval, .TimerFlag, .Restart, .SpeedAdjustPin
MILLIS, 5ul, ENABLED, YES, 0
};
//========================
makeTIMER commonTIMER =
{
//.TimerType, .Interval, .TimerFlag, .Restart, .SpeedAdjustPin
MILLIS, 500ul, DISABLED, NO, 0
};
// c l a s s m a k e I n p u t
//================================================^================================================
//
//a class to define "Input" objects, switches or sensors
//================================================
class makeInput
{
#define NOTvalidated 0
#define VALIDATED 1
#define NOchange 2
private:
public:
static byte s_filter;
//say the above validating "s_filter" variable is set to 10
//if we scan "inputs" every 5ms
//i.e. we sample our inputs every 5ms looking for a change in state.
//5ms * 10 = 50ms is needed to validate a switch change in state.
//i.e. a switch change in state is valid "only after" 10 identical changes are detected.
//This technique is used to filter out EMI (spikes), noise, etc.
//i.e. we ignore switch changes in state that are less than 50ms.
unsigned long switchTime; //the time the switch was closed
byte counter; //a counter used for validating a switch change in state
//these "members" are needed to define an "Input"
byte pin; //the digital input pin number
byte lastState; //the state the input was last in
//================================================
//constructor with parameters
makeInput(byte _pin, byte _lastState)
{
pin = _pin;
lastState = _lastState;
switchTime = 0;
counter = 0;
pinMode(pin, INPUT_PULLUP);
}
//================================================
//condition returned: NOTvalidated (0), VALIDATED (1) or NOchange (2)
//check to see if the input object has had a valid state change
byte validChange()
{
byte currentState = digitalRead(pin);
//===================================
//has there been an input change in state ?
if (lastState != currentState)
{
//we have had another similar change in state
counter++;
//is the "change in state" stable ?
if (counter >= s_filter)
{
//an input change has been validated
//get ready for the next scanning sequence
counter = 0;
//update to this new state
lastState = currentState;
if (currentState == CLOSED)
{
//capture the time when the switch closed
switchTime = millis();
}
return VALIDATED;
}
return NOTvalidated;
}
//===================================
//there has not been an input change in state
counter = 0;
return NOchange;
} //END of validChange()
}; //END of class makeInput
//================================================
//a change in state is confirmed/validated when 10 identical state changes in a row is seen
byte makeInput::s_filter = 10;
// G P I O s A n d V a r i a b l e s
//================================================^================================================
//
//Analogs
//================================================
//
//INPUTS
//================================================
//
//============ GPIO 3
makeInput FWGear =
{
//.pin, .lastState
3, OPENED
};
//============ GPIO 4
makeInput BWGear =
{
//.pin, .lastState
4, OPENED
};
//============ GPIO 5
makeInput NGear =
{
//.pin, .lastState
5, OPENED
};
//============ GPIO 6
makeInput RGear =
{
//.pin, .lastState
6, OPENED
};
//OUTPUTS
//================================================
//
// led pins
const byte NLed = 7;
const byte G1Led = 8;
const byte G2Led = 9;
const byte G3Led = 10;
const byte G4Led = 11;
const byte G5Led = 12;
const byte heartbeatLED = 13;
const byte G6Led = 14;
const byte G7Led = 15;
const byte G8Led = 16;
const byte myOutputs[] = {7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
//VARIABLES
//================================================
//
const unsigned long shortPushTime = 500ul;
const unsigned long longPushTime = 2000ul;
int GearCount = 0;
//the LEDs we will control
enum GEAR : byte
{
Reverse, Neutral, First, Second, Third, Fourth, Fifth, Sixth, Seventh
};
// s e t u p ( )
//================================================^================================================
//
void setup()
{
Serial.begin(9600);
//initialize outputs
for (byte x = 0; x < sizeof(myOutputs); x++)
{
digitalWrite(myOutputs[x], LEDoff);
pinMode(myOutputs[x], OUTPUT);
}
} //END of setup()
// l o o p ( )
//================================================^================================================
//
void loop()
{
//======================================================================== T I M E R heartbeatLED
//condition returned: STILLtiming, EXPIRED or TIMERdisabled
//is it time to toggle the heartbeat LED ?
if (heartbeatTIMER.checkTIMER() == EXPIRED)
{
//toggle the heartbeat LED
digitalWrite(heartbeatLED, digitalRead(heartbeatLED) == HIGH ? LOW : HIGH);
}
//======================================================================== T I M E R switches
//condition returned: STILLtiming, EXPIRED or TIMERdisabled
//is it time to check our switches ?
if (switchesTIMER.checkTIMER() == EXPIRED)
{
checkSwitches();
}
//======================================================================== T I M E R common
//condition returned: STILLtiming, EXPIRED or TIMERdisabled
//is it time to check our switches ?
if (commonTIMER.checkTIMER() == EXPIRED)
{
//we are now finished with this TIMER
commonTIMER.disableTIMER();
//turn OFF the LEDS
for (byte x = 0; x < sizeof(myOutputs) ; x++)
{
digitalWrite(myOutputs[x], LEDoff);
}
}
//================================================
// Other non blocking code goes here
//================================================
} //END of loop()
// c h e c k S w i t c h e s ( )
//================================================^================================================
//
//we have access to:
//object.validChange() - checks to see if there was a valid state change
//object.pin - input hardware pin number
//object.lastState - the state the input was/is in
//object.switchTime - unsigned long variable where we can save millis()
void checkSwitches()
{
//are we currently Timing ?
if (commonTIMER.checkTIMER() == TIMERdisabled)
{
//======================================================================== FWGear
//was there a valid input change ?
//condition returned: NOTvalidated (0), VALIDATED (1) or NOchange (2)
if (FWGear.validChange() == VALIDATED)
{
//========================
//was this switch closed ?
if (FWGear.lastState == CLOSED && GearCount < 8)
{
GearCount = GearCount + 1;
handleLEDs();
}
} //END of FWGear
//======================================================================== BWGear
//was there a valid input change ?
//condition returned: NOTvalidated (0), VALIDATED (1) or NOchange (2)
if (BWGear.validChange() == VALIDATED)
{
//========================
//was this switch closed ?
if (BWGear.lastState == CLOSED && GearCount > 1)
{
GearCount = GearCount - 1;
handleLEDs();
}
} //END of BWGear
//======================================================================== NGear
//was there a valid input change ?
//condition returned: NOTvalidated (0), VALIDATED (1) or NOchange (2)
if (NGear.validChange() == VALIDATED)
{
//========================
//was this switch closed ?
if (NGear.lastState == CLOSED && GearCount > -1)
{
GearCount = 1;
handleLEDs();
}
} //END of BWGear
//======================================================================== RGear
//was there a valid input change ?
//condition returned: NOTvalidated (0), VALIDATED (1) or NOchange (2)
if (RGear.validChange() == VALIDATED)
{
//========================
//was this switch closed ?
if (RGear.lastState == CLOSED && GearCount > 0)
{
GearCount = 0;
handleLEDs();
}
} //END of RGear
} //END of if (commonTIMER.checkTIMER() == TIMERdisabled)
} //END of checkSwitches()
// h a n d l e L E D s ( )
//================================================^================================================
//Reverse, Neutral, First, Second, Third, Fourth, Fifth, Sixth, Seventh
void handleLEDs()
{
switch (GearCount)
{
//============
case Reverse:
{
digitalWrite(NLed, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Neutral:
{
digitalWrite(G1Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case First:
{
digitalWrite(G2Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Second:
{
digitalWrite(G3Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Third:
{
digitalWrite(G4Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Fourth:
{
digitalWrite(G5Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Fifth:
{
digitalWrite(G6Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Sixth:
{
digitalWrite(G7Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
//============
case Seventh:
{
digitalWrite(G8Led, LEDon);
commonTIMER.setInterval(500ul);
commonTIMER.enableRestartTIMER();
}
break;
} //END switch/case
} //END of handleLEDs()
//
//================================================^================================================
//
- Those LEDs are very bright, are you 100% sure those resistors are not 33 ohms ?