Hi Guys,
Please help me understand the if flow instruction better. In my code I have 4 input sensor switches configured to PULLUP state so I can just connect a position switch to GND when TRUE.
The digital outputs switch 4 IGBT`s through driver boards to a small DC motor and in this case it runs on 50% duty cycle, simply for this example.
Is this the best way to handle the LOOP program?
Rgds
Cobus Verster.
#include <arduino.h>
#include <stdio.h>
const int Q1 = 2;// switch for mtr 1
const int Q2 = 3;// switch for mtr 2
const int Q3 = 4;// switch for mtr 3
const int Q4 = 5;// switch for mtr 4
const int posA = 47;// position sensor for mtr 1
const int posB = 49;// position sensor for mtr 2
const int posC = 51;// position sensor for mtr 3
const int posD = 53;// position sensor for mtr 4
void setup()
{
pinMode(Q1, OUTPUT);
pinMode(Q2, OUTPUT);
pinMode(Q3, OUTPUT);
pinMode(Q4, OUTPUT);
pinMode(posA, INPUT_PULLUP);//reverses the input logic.
pinMode(posB, INPUT_PULLUP);//reverses the input logic.
pinMode(posC, INPUT_PULLUP);//reverses the input logic.
pinMode(posD, INPUT_PULLUP);//reverses the input logic.
}
void loop()
{
int posAstate = digitalRead(posA);
int posBstate = digitalRead(posB);
int posCstate = digitalRead(posC);
int posDstate = digitalRead(posD);
if (posAstate == LOW)// position is true
{
digitalWrite(Q1, HIGH);//run small DC motor on 50% duty cycle.
delayMicroseconds(25);
digitalWrite(Q1, LOW);
delayMicroseconds(25);
}
if (posBstate == LOW)// position is true
{
digitalWrite(Q2, HIGH);//run small DC motor on 50% duty cycle.
delayMicroseconds(25);
digitalWrite(Q2, LOW);
}
if (posCstate == LOW)// position is true
{
digitalWrite(Q3, HIGH);//run small DC motor on 50% duty cycle.
delayMicroseconds(25);
digitalWrite(Q3, LOW);
}
if (posDstate == LOW)// position is true
{
digitalWrite(Q4, HIGH);//run small DC motor on 50% duty cycle.
delayMicroseconds(25);
digitalWrite(Q4, LOW);
delayMicroseconds(25);
}}