Hi, I am very much a novice at this and was wandering if I could get some advice here.
I am trying to get a code on my arduino that will read the state on a digital pin and run through a different sequence of simple digitalWrite events depending on if the state is high or low. The part I am having a hard time with is I need it to only go through the sequence once every time the pin changes it's state. The current code I have here continuously loops the code during each state.
I tried using millis() to make it only run until it hit the end of the interval, but then after that it wouldn't run again. I tried for a considerable amount of time to use an interrupt as well, but I just couldn't get it to work.
Here is my current basic code:
int tractorState = 1;
int statePin = 7;
int highPin = 13;
int starterPin = 6;
int ptoForward = 4;
int ptoReverse = 3;
int throttleForward = 8;
int throttleReverse = 5;
void setup() {
Serial.begin(9600);
pinMode(ptoForward,OUTPUT);
pinMode(ptoReverse,OUTPUT);
pinMode(throttleForward,OUTPUT);
pinMode(throttleReverse,OUTPUT);
pinMode(starterPin,OUTPUT);
pinMode(highPin,OUTPUT);
pinMode(tractorState,INPUT);
}
void loop() {
tractorState = digitalRead(statePin);
digitalWrite(highPin,HIGH);
if (tractorState == 1 )
{
starttractor();
startpto();
increasethrottle();
}
else if(tractorState == 0)
{
stoptractor();
stoppto();
decreasethrottle();
}
}
void starttractor(){
digitalWrite(starterPin,HIGH);
}
void startpto(){
digitalWrite(ptoForward, HIGH);
delay(10000);
digitalWrite(ptoForward, LOW);
}
void increasethrottle(){
digitalWrite(throttleForward,HIGH);
delay(10000);
digitalWrite(throttleForward,LOW);
}
void stoptractor(){
digitalWrite(starterPin,LOW);
}
void stoppto(){
digitalWrite(ptoReverse, HIGH);
delay(10000);
digitalWrite(ptoReverse, LOW);
}
void decreasethrottle(){
digitalWrite(throttleReverse,HIGH);
delay(10000);
digitalWrite(throttleReverse,LOW);
}
Any feedback would be great. Thanks.