Go Down

Topic: How to use digitalread to call subroutines? (Read 231 times) previous topic - next topic

viorambler

Mar 21, 2017, 04:43 am Last Edit: Mar 21, 2017, 04:48 am by viorambler
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?

Code: [Select]
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);
}

larryd

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

How are the switches wired?

.
No technical PMs.
The last thing you did is where you should start looking.

viorambler

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.

larryd

Show us a good schematic of your circuit. Show us a good image of your wiring.
No technical PMs.
The last thing you did is where you should start looking.

viorambler

#4
Mar 21, 2017, 05:29 am Last Edit: Mar 21, 2017, 05:35 am by viorambler
Here it is.

cattledog

#5
Mar 21, 2017, 05:47 am Last Edit: Mar 21, 2017, 05:49 am by cattledog
Quote
When the sensor is triggered, the voltage will rise high
Code: [Select]
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
Code: [Select]
if ( digitalRead(tr1pin) == LOW && digitalRead(tr2pin) == LOW && digitalRead(tr3pin) == LOW)

GoForSmoke

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

Code: [Select]

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.
Nick Gammon on multitasking Arduinos:
1) http://gammon.com.au/blink
2) http://gammon.com.au/serial
3) http://gammon.com.au/interrupts

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

TomGeorge

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...... :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up