Hi, I'm going to make a device. When 3 input pins voltage rise high, then call subroutines. But the subroutines always start automatically without inputs being triggered.
How could i make subroutines don't start automatically?
int clkPin = 6; // clk pin9 to pin6
int rePin = 5; // reset pin10 to pin5
int m0Pin = 4; // mode0 pin8 to pin4
int enPin = 3; // en pin7 to pin3
int tr1pin = 8; // set TR1 as pin8
int tr2pin = 9; // set TR2 as pin9
int tr3pin = 10; // set TR3 as pin10
void setup()
{
pinMode(clkPin, OUTPUT); // pin 6 as output
pinMode(rePin, OUTPUT); // pin 5 as input
pinMode(m0Pin, OUTPUT); // pin 4 as input
pinMode(enPin, OUTPUT); // pin 3 as input
pinMode(tr1pin, INPUT_PULLUP); // set TR1(pin8) as input pullup
pinMode(tr2pin, INPUT_PULLUP); // set TR2(pin9) as input pullup
pinMode(tr3pin, INPUT_PULLUP); // set TR3(pin10) as input pullup
}
//
void loop()
{
//motor start at mode0=0
//when sensors are triggered motor start
if ( digitalRead(tr1pin) == HIGH && digitalRead(tr2pin) == HIGH && digitalRead(tr3pin) == HIGH)
initial();
}
//sub1
void initial()
{
//set reset and enbale as 1
delay(2000);
digitalWrite(m0Pin, LOW);
digitalWrite(rePin, HIGH);
digitalWrite(enPin, HIGH);
delay(2000);
digitalWrite(clkPin, LOW);
delay (1000);
digitalWrite(clkPin, HIGH);
delay (1000);
digitalWrite(clkPin, LOW);
delay (500);
digitalWrite(clkPin, HIGH);
delay (500);
digitalWrite(clkPin, LOW);
delay (500);
digitalWrite(clkPin, HIGH);
delay (500);
int i;
for (i=0;i<1000;i++)
{
forward(); // loop satble for 300 times
}
digitalWrite(clkPin, LOW);
delay (500);
digitalWrite(clkPin, HIGH);
delay (500);
digitalWrite(clkPin, LOW);
delay (500);
digitalWrite(clkPin, HIGH);
delay (500);
digitalWrite(clkPin, LOW);
delay (1000);
digitalWrite(clkPin, HIGH);
delay (1000);
}
//sub2
void forward()
{
digitalWrite(clkPin, LOW);
delay (15);
digitalWrite(clkPin, HIGH);
delay (15);
}
When the sensor is triggered, the voltage will rise high
pinMode(tr1pin, INPUT_PULLUP); // set TR1(pin8) as input pullup
pinMode(tr2pin, INPUT_PULLUP); // set TR2(pin9) as input pullup
pinMode(tr3pin, INPUT_PULLUP); // set TR3(pin10) as input pullup
Your input pins are configured as INPUT_PULLUP.
They will read high, unless something in the sensor is switched to ground.
Try reversing the logic