0
votes

The purpose of the following Arduino code is to interface with the high and low signals sent from a Raspberry Pi; the full explanation is rather complex so I'll spare you the waste in time. The signals sent from the Pi (pins 10 and 11) turn a stepper motor connected to an A4988 driver clockwise or counterclockwise. The pins that dictate this out of the Arduino are the step and direction pins (9 and 8). What I am trying to accomplish is to enable the sleepPin after 60 seconds of pin 10 and 11 inactivity.

Likewise, in the same fashion, I want to stop accepting input from pin 10 and 11 if they both read the same input signal for more than 3 seconds. I've looked up methods on how to incorporate time into Arduino script but do not know how to incorporate it in this instance.

byte directionPin = 9;
byte stepPin = 8;
byte sleepPin = 12;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;


boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;
long previousMillis = 0;
long interval = 1000;


void setup() { 
//determines length of stepper movement
pinMode(directionPin, OUTPUT);
pinMode(stepPin, OUTPUT);
//moves motors clockwise or counterclockwise
pinMode(buttonCWpin, INPUT_PULLUP);
pinMode(buttonCCWpin, INPUT_PULLUP);

}

void loop() { 

 readButtons();
 actOnButtons();

}

void readButtons() {

 buttonCCWpressed = false;
 buttonCWpressed = false;


 if (digitalRead(buttonCWpin) == LOW) {
 buttonCWpressed = true;
 }
 if (digitalRead(buttonCCWpin) == LOW) {
 buttonCCWpressed = true;
 }


}

void actOnButtons() {

  if (buttonCWpressed == true) {
 digitalWrite(directionPin, LOW);
   for(int x = 0; x < 1; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(515); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(515); 
   }
 }

 if (buttonCCWpressed == true) {
 digitalWrite(directionPin, HIGH);
   for(int x = 0; x < 1; x++) {
    digitalWrite(stepPin,HIGH); 
    delayMicroseconds(515); 
    digitalWrite(stepPin,LOW); 
    delayMicroseconds(515); 
   }
 }






}

Any help would be greatly appreciated along with any tips or concerns.

Thank You.

1

1 Answers

0
votes

If you have the luxury to use pins 2 and 3 rather than pins 10 and 11 (assuming you have an Arduino Uno for instance), it could be useful to work with external interrupts.

As a solution for your first problem, here's a minimal code that should put the sleep pin high after 60 seconds of inactivity on both direction pins 2 & 3:

volatile long int last_activity;

void setup(){
    attachInterrupt(2, tstamp, CHANGE);
    attachInterrupt(3, tstamp, CHANGE);
    pinMode(2, INPUT); // your new CW pin
    pinMode(3, INPUT); // your new CCW pin
    pinMode(12, OUTPUT);
    digitalWrite(12, LOW);
    last_activity = millis();
}

void loop(){
    if (millis() - last_activity > 60e3) {
        digitalWrite(12, HIGH);
        // do some other things...
    }
}

void tstamp(){
    last_activity = millis();
}

Now for your second problem, what exactly do you mean by "stop accepting input from pins 10 and 11"? If you only need to check that their state is the same, adding volatile long int last_common_state; in the preamble and checking for digitalRead(2) == digitalRead(3); in tstamp()'s body to update last_common_state should get you on the right track.