I have attempted to read ALL the switches and their current states first [ (limits_u1_State = digitalRead(limits_u1); ] then move the first motor function [ void moveMotor1() ] but I can't seem to get it working. I believe I am properly recording the states of the limit switches, and I tried implemented the if {} else {} statements and many variations of it instead of while{}...
Here is the main part of the code I am working on:
void loop() //mobile 1
{ //call the functions that do the work
limitsw_u1_State = digitalRead(limitsw_u1); // check if the pushbutton is pressed.
limitsw_d1_State = digitalRead(limitsw_d1); // if it is, the buttonState changes to HIGH:
digitalRead(limitsw_u2_State);
digitalRead(limitsw_d2_State);
digitalRead(limitsw_u3_State);
digitalRead(limitsw_d3_State);
digitalRead(limitsw_u4_State);
digitalRead(limitsw_d4_State);
}
//====================
void moveMotor1(){
if (limitsw_u1_State == HIGH) //upward limitswitch pressed
{
digitalWrite(down1, HIGH); //make motor1 go down
digitalWrite(up1, LOW);
}
else if (limitsw_d1_State == HIGH) //downward limit switch pressed
{
digitalWrite(up1, HIGH); //make motor1 go up
digitalWrite(down1, LOW);
}
}
And here is the full (very long) code from the beginning:
//constants
const int up1 =13; //relays that trigger movement of DC motor
const int down1 =12;
const int up2 =11;
const int down2 =10;
const int up3 =9;
const int down3 =8;
const int up4 =7;
const int down4 =6;
const int limitsw_u1 =5; // input switches that change movement of DC motor
const int limitsw_d1 =4;
const int limitsw_u2 =3;
const int limitsw_d2 =2;
const int limitsw_u3 =1;
const int limitsw_d3 =0;
const int limitsw_u4 =A5;
const int limitsw_d4 =A4;
const int changeDuration = 500; //amount of delay time for reversal
//variables will change
byte limitsw_u1_State = LOW; // used to record which direction the mobile is moving
byte limitsw_d1_State = LOW;
byte limitsw_u2_State = LOW;
byte limitsw_d2_State = LOW;
byte limitsw_u3_State = LOW;
byte limitsw_d3_State = LOW;
byte limitsw_u4_State = LOW;
byte limitsw_d4_State = LOW;
void setup() {
// relays
pinMode(up1, OUTPUT); //Que relays (7&8) to rotate motor.1 counterclockwise
pinMode(down1, OUTPUT); //Que relays (5&6) to rotate motor.1 clockwise
pinMode(up2, OUTPUT); // Relays (1&2)
pinMode(down2, OUTPUT); // Relays (3&4)
pinMode(up3, OUTPUT); // Relays (7&8)
pinMode(down3, OUTPUT); // Relays (5&6)
pinMode(up4, OUTPUT); // Relays (1&2)
pinMode(down4, OUTPUT); // Relays (3&4)
// limit switches
pinMode(limitsw_u1, INPUT); //Up limitswitch on motor.1 breadpin (
pinMode(limitsw_d1, INPUT); //Down limitswitch 1 breadpin (
pinMode(limitsw_u2, INPUT); //Up limitswitch on motor.2 breadpin (
pinMode(limitsw_d2, INPUT); //Down limitswitch 2 breadpin (
pinMode(limitsw_u3, INPUT); //Up limitswitch on motor.3 breadpin (
pinMode(limitsw_d3, INPUT); //Down limitswitch 3 breadpin (
pinMode(limitsw_u4, INPUT); //Up limitswitch on motor.4 breadpin (
pinMode(limitsw_d4, INPUT); //Down limitswitch 4 breadpin (
Serial.begin(9600); //Initialize Serial connection
}
void loop() //mobile 1
{ //call the functions that do the work
limitsw_u1_State = digitalRead(limitsw_u1); // check if the pushbutton is pressed.
limitsw_d1_State = digitalRead(limitsw_d1); // if it is, the buttonState changes to HIGH:
digitalRead(limitsw_u2_State);
digitalRead(limitsw_d2_State);
digitalRead(limitsw_u3_State);
digitalRead(limitsw_d3_State);
digitalRead(limitsw_u4_State);
digitalRead(limitsw_d4_State);
}
//====================
void moveMotor1(){
if (limitsw_u1_State == HIGH) //upward limitswitch pressed
{
digitalWrite(down1, HIGH); //make motor1 go down
digitalWrite(up1, LOW);
}
else if (limitsw_d1_State == HIGH) //downward limit switch pressed
{
digitalWrite(up1, HIGH); //make motor1 go up
digitalWrite(down1, LOW);
}
}
This other code that is designed for 1 motor function still works whenever I plug it in to check if the system is working:
{
limitsw_u1_State = digitalRead(limitsw_u1);
{
// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (limitsw_u1_State == HIGH)
while((limitsw_d1_State = digitalRead(limitsw_d1)) != HIGH) {
// turn motor counterclockwise on:
digitalWrite(up1, HIGH);
digitalWrite(down1, LOW);
}
{
limitsw_d1_State = digitalRead(limitsw_d1);
{
if (limitsw_d1_State == HIGH)
while((limitsw_u1_State = digitalRead(limitsw_u1)) != HIGH) {
// rotate clockwise slow speed
digitalWrite(down1, HIGH);
digitalWrite(up1, LOW);
}
}
}
}}
Paul_KD7HB:
Also, you will probably need to stop the box movement before beginning to move in the opposite direction.
Paul
It doesn't seem like I need a stop function before beginning to move in the opposite direction. I have tested the last code I posted with the motor and mobile object and it seems to reverse fine.