jremington:
Looks great, glad you got it working!
not only that but also managed to get my led blinking the many of tumes ive clicked the button so i can know its state.
const int inPin = 3; // tilt switch or button to trigger dc motor
const int outPin = 1; // dc motor
const byte buttonPin = 2; //button to change pwm
const byte ledPin = 0; // led to know the state of pwm
unsigned long startTime;
unsigned long period = 100;
boolean flashing = false;
byte count = 0;
byte previousButtonState;
int pwmvalue = 0;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
int tiltPushCounter = 0;
int tiltState = 0;
int lasttiltState = 0;
void setup()
{
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(inPin, INPUT);
pinMode(outPin, OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(outPin, LOW);
}
void loop()
{
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter ++;
} else {
// if the current state is LOW then the button
// wend from on to off:
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lastButtonState = buttonState;
switch (buttonPushCounter) {
case 0:
digitalWrite(outPin, LOW);
digitalWrite(ledPin, LOW);
pwmvalue = 0;
break;
case 1:
pwmvalue = 255;
if (!flashing)
{
byte currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState && currentButtonState == HIGH)
{
flashing = true;
count = 0;
startTime = millis();
}
previousButtonState = currentButtonState;
}
else
{
unsigned long currentTime = millis();
if (currentTime - startTime >= period)
{
digitalWrite(ledPin, !digitalRead(ledPin));
startTime = currentTime;
count++;
if (count == 2)
{
flashing = false;
}}}
break;
case 2:
pwmvalue = 180;
if (!flashing)
{
byte currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState && currentButtonState == HIGH)
{
flashing = true;
count = 0;
startTime = millis();
}
previousButtonState = currentButtonState;
}
else
{
unsigned long currentTime = millis();
if (currentTime - startTime >= period)
{
digitalWrite(ledPin, !digitalRead(ledPin));
startTime = currentTime;
count++;
if (count == 4)
{
flashing = false;
}}}
break;
case 3:
pwmvalue = 100;
if (!flashing)
{
byte currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState && currentButtonState == HIGH)
{
flashing = true;
count = 0;
startTime = millis();
}
previousButtonState = currentButtonState;
}
else
{
unsigned long currentTime = millis();
if (currentTime - startTime >= period)
{
digitalWrite(ledPin, !digitalRead(ledPin));
startTime = currentTime;
count++;
if (count == 6)
{
flashing = false;
}}}
break;
case 4:
pwmvalue = 70;
if (!flashing)
{
byte currentButtonState = digitalRead(buttonPin);
if (currentButtonState != previousButtonState && currentButtonState == HIGH)
{
flashing = true;
count = 0;
startTime = millis();
}
previousButtonState = currentButtonState;
}
else
{
unsigned long currentTime = millis();
if (currentTime - startTime >= period)
{
digitalWrite(ledPin, !digitalRead(ledPin));
startTime = currentTime;
count++;
if (count == 8)
{
flashing = false;
}}}
break;
case 5:
buttonPushCounter = 0;
digitalWrite(outPin, LOW);
break;
}
// read the pushbutton input pin:
tiltState = digitalRead(inPin);
// compare the buttonState to its previous state
if (tiltState != lasttiltState) {
// if the state has changed, increment the counter
if (tiltState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
tiltPushCounter ++;
}
else {
}
// Delay a little bit to avoid bouncing
delay(50);
}
// save the current state as the last state,
//for next time through the loop
lasttiltState = tiltState;
// turns on the LED every four button pushes by
// checking the modulo of the button push counter.
// the modulo function gives you the remainder of
// the division of two numbers:
switch (tiltPushCounter) {
case 0:
digitalWrite(outPin, LOW);
break;
case 1:
analogWrite(outPin, pwmvalue);
delay(300);
break;
case 2:
tiltPushCounter = 0;
break;
}}