How to use digitalread to call subroutines?

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);
}

if ( digitalRead(tr1pin) == HIGH && digitalRead(tr2pin) == HIGH && digitalRead(tr3pin) == HIGH)
{
initial();
}

How are the switches wired?

.

LarryD:
if ( digitalRead(tr1pin) == HIGH && digitalRead(tr2pin) == HIGH && digitalRead(tr3pin) == HIGH)
{
initial();
}

How are the switches wired?

.

It's a kind of sensor, 5V, GND, and signal output. When the sensor is triggered, the voltage will rise high. I wire output pin directly to arduino.

Show us a good schematic of your circuit. Show us a good image of your wiring.

Here it is.

擷取.JPG

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

if ( digitalRead(tr1pin) == LOW && digitalRead(tr2pin) == LOW && digitalRead(tr3pin) == LOW)

viorambler:
When 3 input pins voltage rise high, then call subroutines.

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

INPUT_PULLUP makes the pin HIGH unless it is grounded through much less than 20K resistance.

You want INPUT instead of INPUT_PULLUP for When 3 input pins voltage rise high.

You want INPUT instead of INPUT_PULLUP for When 3 input pins voltage rise high.

You may also need external pulldown resistors, if whatever is connected to the pin does not pull it down.

Hi,
Can you post a link to the devices that you have connected to the Arduino input so we can see how their outputs are configured?

Have you got a DMM to show the inputs are going HIGH.

Tom...... :slight_smile: