I wrote a code to activate MOSFETs in series. There is only one push-button, on every press of the button, the next FET has to be activated. Running code on an ATtiny45
What I want the code to do is, when the button is pressed once, irrespective of the button's ON state duration, only the respective FET must be activted.
The problem with my code is, if the button is pressed and held there for longer than a few milliseconds, the control activates the corresponding FET and moves on to the next one. If the button is held pressed long enough, the control runs through all MOSFETs on just one button press. How do I stop this from happening?
unsigned long rxTrigger;
int rxPreset;
int potVal;
int Buzz = 3;
int count;
int Sw;
void setup()
{
// Rx signal input
pinMode (0, OUTPUT);
pinMode (1, OUTPUT);
pinMode (2, INPUT);
pinMode (3, OUTPUT);
pinMode (4, OUTPUT);
analogWrite (Buzz, 1024); // Initialization tone
delay (10);
analogWrite (Buzz, LOW);
delay (20);
analogWrite (Buzz, 1024);
delay (10);
analogWrite (Buzz, LOW);
delay (20);
analogWrite (Buzz, 1024);
delay (10);
analogWrite (Buzz, LOW);
}
void loop()
{
rxTrigger = pulseIn (2, HIGH); // Read Rx signal input
if (rxTrigger > 1300)
{
Sw = HIGH;
}
else
{
Sw = LOW;
}
if (Sw == HIGH)
{
count ++;
}
if (count > 3)
{
count = 0;
}
if (count == 0)
{
analogWrite (Buzz, 1024);
delay (200);
analogWrite (Buzz, LOW);
digitalWrite (0, LOW); // switch off all MOSFETs
digitalWrite (1, LOW);
digitalWrite (4, LOW);
delay (150);
}
else if (count == 1)
{
analogWrite (Buzz, 1024);
delay (200);
analogWrite (Buzz, LOW);
digitalWrite (0, HIGH);
digitalWrite (1, LOW);
digitalWrite (4, LOW);
delay (150);
}
else if (count == 2)
{
analogWrite (Buzz, 1024);
delay (200);
analogWrite (Buzz, LOW);
digitalWrite (1, HIGH);
digitalWrite (4, LOW);
digitalWrite (0, LOW);
delay (150);
}
else if (count == 3)
{
analogWrite (Buzz, 1024);
delay (200);
analogWrite (Buzz, LOW);
digitalWrite (4, HIGH);
digitalWrite (1, LOW);
digitalWrite (0, LOW);
delay(150);
}
}