Hi, I need in adding a push button to my project
I’m guessing this is either homework or a school project due tomorrow, hence the urgency?
What are you turning off, the motor or everything?
If it’s just the motor then using another pin (let’s go with pin 8) set pinMode to pin 8 as INPUT_PULLUP inside setup().
Then inside loop(), you need to read that pin using digitalRead(8) and compare it to LOW.
You may also want a global Boolean flag, call it “isOn” initially set to FALSE.
Then once you read pin 8, you toggle isOn. Simple bitwise logic will work.
Once you have all that in place, you use an IF/ELSE statement to set analogWrite for pin 9 to either turn on your transistor with your PWM signal or set it to output 0 ie. Off.
Note: you can reduce the IF/ELSE to just “isOn ? y : 0”. An inline compact IF/ELSE statement
You want a button which when pressed toggles the motor on/off. The starting state is that the motor is off. Is that correct ?
Which Arduino pin do you want to use for the button and how are you going to wire the button ?
EDIT
post crossed with @HazardsMind
I am occurring with a problem with, what I am doing wrong?
`<void setup()
{
Serial.begin(9600);
pinMode(9,OUTPUT );
pinMode(LEDpin,INPUT_PULLUP); // set the pushbutton pin as input with internal pull-up resistor }
}
void loop(){
int x = analogRead(A0);
int y = map(x, 0, 1023, 0, 255);
analogWrite(9, y);
state = digitalRead(LEDpin);
if(state == LOW){ // button is pressed digitalWrite(9, HIGH); } else{ // button is not pressed digitalWrite(9, LOW); } }
state=digitalRead(LEDpin);
}
}/>`
posting twice some code without the code tags may be?
➜ do yourself a favour and please read How to get the best out of this forum and post accordingly
--
add the switch on the power line... No current, nothing working, and no code to maintain.
I have done what you said, can you check my code @jim-p
void setup()
{
Serial.begin(9600);
pinMode(9,OUTPUT );
pinMode(LEDpin,INPUT_PULLUP); // set the pushbutton pin as input with internal pull-up resistor }
}
void loop(){
int x = analogRead(A0);
int y = map(x, 0, 1023, 0, 255);
analogWrite(9, y);
state = digitalRead(LEDpin);
if(state == LOW){ // button is pressed digitalWrite(9, HIGH); } else{ // button is not pressed digitalWrite(9, LOW); } }
state=digitalRead(LEDpin);
}
}
you have code caught in the comment (after the // on the same line)
Still an error even if you fix it`
void setup()
{
Serial.begin(9600);
pinMode(9,OUTPUT );
pinMode(LEDpin,INPUT_PULLUP); // set the pushbutton pin as input with internal pull-up resistor }
}
void loop(){
int x = analogRead(A0);
int y = map(x, 0, 1023, 0, 255);
analogWrite(9, y);
state = digitalRead(LEDpin);
if(state == LOW){ // button is pressed digitalWrite(9, HIGH); }
else{ // button is not pressed digitalWrite(9, LOW); } }
state=digitalRead(LEDpin);
}
}
Try this code and see if the UNO LED turns off/on when you push the button
const int BUTTON_PIN = 2; // Arduino pin connected to button's pin
byte ledState = LOW; // The current state of The builtin LED
byte lastButtonState; // The previous state of button
byte currentButtonState; // The current state of button
void setup()
{
Serial.begin(9600);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Set arduino pin to input pull-up mode
pinMode(LED_BUILTIN, OUTPUT); // Set arduino pin to output mode
lastButtonState = digitalRead(BUTTON_PIN); // Read the Button
}
void loop()
{
currentButtonState = digitalRead(BUTTON_PIN); // Read the new state
if (lastButtonState == HIGH && currentButtonState == LOW)
{
Serial.println("The button is pressed");
ledState = !ledState; // Toggle state of LED
}
lastButtonState = currentButtonState; // Save the last state
// control LED arccoding to the toggled state
digitalWrite(LED_BUILTIN, ledState);
delay(50);
}
you still have code within comments
I was going to suggest using the techniques in the Arduino IDE example 'StateChangeDetection', but Jim-p has already done it for you.
Jim-p's code only turns the LED on/off when you press the button.
You can add your code to read the potentiometer and adjust the motor speed when the LED is on, and turn off the motor when the LED is off.
@jim-p The Uno LED turn on when you press the the button and turn off when you press it again, but the motor is not working.
You need to modify Jim's code, by adding parts from your original code.
(I mentioned that in reply #12, but I was still editing it when you posted reply #13).
That is the next part.
Do you think you can figure out how to do it?
Hint: ledState = 1 when you first push the button and will = 0 the next time you push it.
@jim-p I have edited your code, is this correct
Try it and see if it works.
@jim-p It works but, it is way to fast for my project runs at 105410rpm, how can i lower it down, can you tell me on what to change in the code?
Even if you turn the pot all the way down?
That's quite an exact figure, how do you know?
Adjust your map() function. Change the 255 to some lower value.