Check out the state change detection example in the IDE under File, Examples, Digital. Use that to run the motor when the state of pin5 CHANGES from low to high only (not WHILE pin 5 IS high).
Already tried waht you guys told me and no luck, still got the motor just running with the key fob pressed always.
// Set digital pin numbers:
const int buttonPin = 5; // The number of the Pushbutton pin
int buttonState = 0; // Variable for reading the pushbutton status
int directionState = 0; // Variable for reading direction of the servo
int Distancia = 0;
void setup() {
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (directionState == 0){
//The button is pushed
if (buttonState == HIGH) {
digitalWrite(2, HIGH);
delayMicroseconds(100);
digitalWrite(2, LOW);
delayMicroseconds(100);
Distancia = Distancia + 1;
if (Distancia == 1600)
{
if (digitalRead(3) == LOW)
{
digitalWrite(3, HIGH);
}
else
{
digitalWrite(3, LOW);
}
Distancia = 0;
delay(1000);
}
}
}
}
But still, something about details only, that i like to put on.
Will it be possible to output the delay in there like a countdown? I have already the delay outputting 3000 but would like it to like 3...2...1 or even 1..2..3.
#include <Stepper.h>
long MyTimer;
const int revs = 3600 ;
boolean wasTriggered = false;
Stepper stepper1(revs, 2,3);
void setup() {
Serial.begin(9600);
stepper1.setSpeed(200); // larger number = faster
pinMode(5, INPUT); // Input for the Wireless Receiver
}
void loop() {
if (digitalRead(5)){
wasTriggered = true;
}
if(wasTriggered){
long myTimer = millis(); //using long type because the value can be very large
Serial.print("Start");
delay(3000);
long timePast = millis() - myTimer; //number of milliseconds since you set myTimer
Serial.print(timePast);
stepper1.step(revs);
delay(1500);
stepper1.step(-revs);
delay(1000);
Serial.print("Stop");
wasTriggered = false;
}
}
It makes no sense to use both millis() and delay(). The Arduino can do nothing during a delay() - that's why you should use millis() for all your timing. Then you can print any countdown you like.