Hello, I’m new to Arduino and I’m working on a project for the kids. I’m making a air soft target course.
I will have seven targets total. I can make it work with just timing. But I would like to add limit switch to stop and start the motor.
So the sequence of events I would like happen are.
-
I press a key and it is tied to a time limit. Example would be if i press w the target or targets will be up for 3 seconds. I press e and they are up for 5 seconds.
-
Checks target down limit switch. If down, turn on the target motor CW.
3.Once target is raised it will stop target motor.
- Then one of two things happen. A. the target times out and lowers. Or B. the target is hit and moves off the up limit switch and lowers.
If this was a plc with ladder logic I could do this all day. But I have been beating my head against a wall so far. If anyone could give me a code example I would be grateful. I have tried to put together different example sketches and can’t seem to get it to do everything I want.
Here is the code I have so far.
int pwm_a = 3; // Channel A speed
int dir_a0 = 4; // Channel A direction 0
int dir_a1 = 5; // Channel A direction 1
int uplimit = 11; //Up Limit Switch
int downlimit = 12; //Down Limit Switch
char inbit; // serial input
void setup() {
Serial.begin(9600);
pinMode(pwm_a, OUTPUT);
pinMode(dir_a0, OUTPUT);
pinMode(dir_a1, OUTPUT);
pinMode(uplimit, INPUT);
pinMode(downlimit, INPUT);
draw(); // Draw the timing instructions to the serial terminal
}
void loop()
{
if(Serial.available()){ // Wait for serial input
inbit = Serial.read();
switch(inbit){ // Switch based on serial in
case ‘w’: // up for 3 seconds
TargetUp(255);
delay(1000);
shutoff();
delay(3000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘s’: // up for 5 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(5000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘e’: // up for 6 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(6000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘d’: // up for 8 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(8000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘r’: // up for 11 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(11000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘f’: // up for 13 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(13000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘x’: // up for 14 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(14000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘c’: // up for 17 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(17000);
TargetDown(255);
delay(1000);
shutoff();
break;
case ‘v’: // up for 19 seconds
TargetUp(255);
delay(3000);
shutoff();
delay(19000);
TargetDown(255);
delay(1000);
shutoff();
break;
}
}
}
void TargetUp(int speed)
{
digitalWrite(dir_a0, 0);
digitalWrite(dir_a1, 1);
analogWrite(pwm_a, speed);
}
void TargetDown(int speed) // Move Backward
{
digitalWrite(dir_a0, 1);
digitalWrite(dir_a1, 0);
analogWrite(pwm_a, speed);
}
void shutoff() // Stop Motors w/o braking
{
digitalWrite(dir_a0, 0);
digitalWrite(dir_a1, 0);
analogWrite(pwm_a, 0);
}
void draw() // Serial Instructions
{
Serial.println(" Target Timer ");
Serial.println(" ");
Serial.println(" ------------------------- ");
Serial.println(" | | | | ");
Serial.println(" | W | E | R | ");
Serial.println(" | 3 sec | 6 sec |11 sec | ");
Serial.println(" ------------------------- ");
Serial.println(" | | | | ");
Serial.println(" | S | D | F | ");
Serial.println(" | 5 sec | 8 sec |13 sec | ");
Serial.println(" ------------------------- ");
Serial.println(" | | | | ");
Serial.println(" | X | C | V | ");
Serial.println(" |14 sec |17 sec |19 sec | ");
Serial.println(" ------------------------- ");
Serial.println(" ");
}