Hi all,
I have just ordered my first Arduino and have been playing around with simulators before it arrives. I have tried to make a circuit that pulses a motor depending on the input from a potentiometer (which works) , and continuously rotates the motor if a button is being pressed.
The issue i'm having is, when i set a delay to pulse the motor, the delay can't be overrun by the button being pressed down. Hopefully, that all makes sense.
Here is a link to simulation if you can help: Login | Tinkercad
Code for Reference,
/*
Input Pullup Serial
This example demonstrates the use of pinMode(INPUT_PULLUP). It reads a
digital input on pin 2 and prints the results to the serial monitor.
OPEN THE SERIAL MONITOR TO SEE THE OUTPUT FROM THE INPUT PIN >>
The circuit:
* Momentary switch attached from pin 2 to ground
* Built-in LED on pin 13
Unlike pinMode(INPUT), there is no pull-down resistor necessary. An internal
20K-ohm resistor is pulled to 5V. This configuration causes the input to
read HIGH when the switch is open, and LOW when it is closed.
created 14 March 2012
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/InputPullupSerial
This example code is in the public domain
*/
void setup() {
//start serial connection
Serial.begin(9600);
//configure pin2 as an input and enable the internal pull-up resistor
pinMode(2, INPUT_PULLUP);
pinMode(A0, INPUT);
pinMode(13, OUTPUT);
}
void loop() {
//read the pushbutton value into a variable
int fishON = digitalRead(2);
int pulse = 0;
//print out the value of the pushbutton
//Serial.println(pulse);
pulse = analogRead(A0);
// Keep in mind the pullup means the pushbutton's
// logic is inverted. It goes HIGH when it's open,
// and LOW when it's pressed. Turn on pin 13 when the
// button's pressed, and off when it's not:
if(fishON == HIGH) {
if (pulse <= 1000) {
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(pulse * 20);
}
else {
digitalWrite(13, LOW);
}
}
if (fishON == HIGH) {
digitalWrite(13, LOW);
} else {
digitalWrite(13, HIGH);
}
}
Kind Regards,
Jordan