I'm in a 2nd year college student making project to set time duration in minutes to turn ON and OFF of MOTOR.
IM stuck in this please help.
Want learn and explore more in coding and arduino use.
Below is my code where im using led right now later i will add motor.
please reply anyone who can guide me.
int button1 =5;
int button2 =6;
int led =4;
int Time=0;
void setup() {
// put your setup code here, to run once:
Serial.print(9600);
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int Onbutton=digitalRead(button1);
int Offbutton=digitalRead(button2);
if(Onbutton==HIGH)
{
Time++;
digitalWrite(led,HIGH);
Serial.print("led ON");
Serial.print(Time);
}
if(Offbutton==HIGH)
{
Time--;
digitalWrite(led,LOW);
Serial.print("led OFF");
Serial.print(Time);
}
}
You haven't said what the problem is, but I going to guess:
a) floating input
b) You're doing things while the button is pressed, not when the button becomes pressed.
Problem I'm facing is when i press button1 it should increment the time per minute and set time for motor to run and then when i press button2 it should decrement time per minute to Off motor.
Automatically it should then ON and OFF the motor till the time arduino have power.
int button1 =5;
int button2 =6;
int led =4;
int Time=0;
int lastButtonState=0;
void setup() {
// put your setup code here, to run once:
Serial.print(9600);
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(led,OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
int Onbutton=digitalRead(button1);
int Offbutton=digitalRead(button2);
if (Onbutton != lastButtonState)
{
if (Onbutton == HIGH)
{
Time++;
digitalWrite(led,HIGH);
Serial.print("led ON");
Serial.print(Time);
}
}
lastButtonState=Onbutton;
if (Offbutton != lastButtonState)
{
if (Offbutton == HIGH)
{
Time--;
digitalWrite(led,LOW);
Serial.print("led OFF");
Serial.print(Time);
}
}
lastButtonState=Offbutton;
}