This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int buttonPin = 3; //Variable for button pin | |
int ledPin[] = {11, 12, 13}; //Variables for the led pins that indicate fan speed | |
int ledPin1 = 6; //Variable for indicating whether motor can be controlled by potentiometer | |
int buttonState1 = 0; //Button state variable | |
int buttonState2 = 0; //Second button state | |
int buttonState = 0; //Another button state variable | |
int buttonCount = 0; //Variable to store number times button is pushed | |
boolean lastButton = HIGH; //Intial state for button using boolean "true" or "false" | |
int potPin = A0; //Pin that potentiometer is connected to | |
int motorPin = 9; //Pin the motor is connected to | |
int potValue; //Value for the potentiometer from 0 to 1023 | |
int motorValue = 0; //Intial motor value from o to 255 | |
int x; | |
void setup() { | |
for (x = 0; x < 3; x++) { //Configure the led pins that indicate fan speed as for loops and outputs | |
pinMode (ledPin[x], OUTPUT);} | |
pinMode(motorPin, OUTPUT); //Configure motor pin as output | |
pinMode(buttonPin, INPUT); //Configure button as input pin | |
} | |
void loop() { | |
buttonState1 = digitalRead(buttonPin); //reads the state of the button and stores the variable | |
delay(50); //delay 50 milliseconds for debounce | |
buttonState2 = digitalRead(buttonPin); //aids in debounce | |
if (buttonState1 == buttonState2){ //if statement (buttonState1 is equal to buttonState2) | |
buttonState = buttonState2; //buttonState will equal buttonState2 | |
if (buttonState != lastButton) { //if buttonState is not lastButton and buttonState is equal to low then add 1 to buttonCount | |
if (buttonState == LOW){ | |
buttonCount++; | |
} | |
} | |
lastButton = buttonState; //Varaible used to store value | |
} | |
if ( (buttonCount % 2) == 0) { //if buttonCount is even then enable the potentiometer to control the motor speed | |
potValue = analogRead(potPin); //read the pot value | |
motorValue = map(potValue, 0, 1023, 0, 255); //maps the value from 0 to 1023 to 0 to 255 (digital to analog) | |
analogWrite(motorPin, motorValue); //tells the motor pin to turn on in accordance to the value read by the pot pin | |
delay(2); | |
} | |
if ( (buttonCount % 2) != 0) { //if buttonCount is odd then do not allow the potentiometer to control the motor speed | |
analogWrite (motorPin, 0); | |
} | |
{ | |
potValue = analogRead(potPin); //make the leds indicating fan speed to turn on indacting states of "high medium or low", | |
for (int i = 0; i < 3; i++) { //high = 3 red leds on, medium = 2 red leds on and low = 1 red led on | |
if( (i * 341) < potValue) { digitalWrite(ledPin[i], HIGH);} | |
else | |
{ digitalWrite(ledPin[i], LOW);} | |
if ( (buttonCount % 2) != 0) { | |
digitalWrite (ledPin[i], LOW); | |
} | |
} | |
} | |
if ( (buttonCount % 2) == 0) { //if buttonCount is even then turn on the led indicating whether you can control motor with potentiometer | |
digitalWrite (ledPin1, HIGH); | |
} | |
else { | |
digitalWrite (ledPin1, LOW); | |
} | |
} | |
No comments:
Post a Comment