[code]
#include<Servo.h>
#define m1 8
#define m2 9
#define EN1 2
#define m3 10
#define m4 11
#define EN2 3
#define m5 12
#define m6 13
#define EN3 4
#define STQ 20
#define Q4 53
#define Q3 52
#define Q2 51
#define Q1 50
#define ECHO 21
#define TRIG 22
long duration;
long distance;
Servo servo1;
void shoot()
{
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
digitalWrite(m3, HIGH);
digitalWrite(m4, LOW);
analogWrite(EN2, 255);
delay(1000);
}
void stop()
{
digitalWrite(m1, LOW);
digitalWrite(m2, LOW);
analogWrite(EN1, 100);
digitalWrite(m3, LOW);
digitalWrite(m4, LOW);
analogWrite(EN2, 100);
delay(1000);
}
void up()
{
digitalWrite(m5, HIGH);
digitalWrite(m6, LOW);
analogWrite(EN3, 200);
delay(500);
}
void down()
{
digitalWrite(m5, LOW);
digitalWrite(m6, HIGH);
analogWrite(EN3, 200);
delay(500);
}
void load()
{
for (int pos = 0; pos <= 90; pos += 1)
servo1.write(pos);
delay(500);
}
void wait()
{
for (int pos = 90; pos >= 0; pos -= 1)
servo1.write(pos);
delay(500);
}
void setup()
{
pinMode(m1, OUTPUT);
pinMode(m2, OUTPUT);
pinMode(EN1, OUTPUT);
pinMode(m3, OUTPUT);
pinMode(m4, OUTPUT);
pinMode(EN2, OUTPUT);
pinMode(m5, OUTPUT);
pinMode(m6, OUTPUT);
pinMode(EN3, OUTPUT);
pinMode(STQ, INPUT);
pinMode(Q4, INPUT);
pinMode(Q3, INPUT);
pinMode(Q2, INPUT);
pinMode(Q1, INPUT);
pinMode(ECHO, OUTPUT);
pinMode(TRIG, INPUT);
servo1.attach(23);
Serial.begin(9600);
// put your setup code here, to run once:
}
void loop()
{
{
int temp1 = digitalRead(Q1);
int temp2 = digitalRead(Q2);
int temp3 = digitalRead(Q3);
int temp4 = digitalRead(Q4);
if (temp1 == 0 && temp2 == 1 && temp3 == 0 && temp4 == 0)
shoot();
else if (temp1 == 1 && temp2 == 1 && temp3 == 0 && temp4 == 0)
up();
else if (temp1 == 0 && temp2 == 0 && temp3 == 1 && temp4 == 0)
load();
else if (temp1 == 1 && temp2 == 0 && temp3 == 1 && temp4 == 0)
stop();
else if (temp1 == 0 && temp2 == 1 && temp3 == 1 && temp4 == 0)
wait();
else if (temp1 == 1 && temp2 == 0 && temp3 == 0 && temp4 == 1)
down();
}
{
digitalWrite(TRIG, LOW);
delay(2);
digitalWrite(TRIG, HIGH);
delay(10);
digitalWrite(TRIG, LOW);
duration = pulseIn(ECHO, HIGH);
distance = duration * 0.034 / 2;
}
shoot();
if (distance <= 20)
delay(2000);
}
[/code]
as per my program the motor pin m1,m2,m3,m4 will help in puching out bullets from the barrel ,the motor pin m5 and m6 will control the shooting barrel up and down while the movement of the servo from 0 degree to 90 degree will load the bullet into the shooting motor and the servo must go to its initial state within 3ms or less i wrote a program for this too but it sound too extensive(time consuming) can anyone suggest me How to automatically move the servo to its initial state without an external interrupt .
Hoping i find help
my humble regard
Lucky
The question is in the code…
What’s the purpose of the 2 second delay ?
if (distance <= 20)
delay(2000);
Now i am not sure but it must do something like this
if any obstacles detected less than 20 cm then the servo must load the bullet within 5 ms and firing the bullet will be for 1 sec , which will be firing two bullet in one second
Now i get you okay my fault i must be 1 sec delay
But instead of controlling the servo from 0 and 90 degree can i automatically move the servo to 0 degree once the bullet is loaded
I am still in your dept
Brother @J-M-L
My regard
Lucky
Shoot does already have a 1s delay built in, so you would be adding to that delay
The whole delay thing is not really needed, this could be coded like a state machine and there would be really no delay in the code
For extra information and examples look at Using millis() for timing. A beginners guide and Several things at the same time
Hello
I´ve studied and tried to unterstand your sketch. My advice to improve readiblity of your sketch first add comments either/or insert variable names with descriptive meaning. Well, I recomment as already mentioned a FSM and a timer function to run the different states, too.
A FSM can be simply built by using the switch/case statement. A timer function can be simply derived from the BWOD example to be found in the IDE.
Thank you
Brother @paulpaulson
For the advice i surely will follow the suggestions given unto me and my program
i been lately learning coding with arduino which i am not very familiar i used to code for microcontroller 8051 , now i will follow any advice from you for learning this code and program in arduino
my humble regard to you
From Lucky
One suggestion for readability and efficiency is to separate the pins by motor.
const byte Motor1[] = {8, 9, 2};
const byte Motor2[] = {10, 11, 3};
const byte Motor3[] = {12, 13, 4};
NOTE: I don't know what each of your motors does but it would be much more readable if the arrays names said what the motor did, like LeftWheel and RightWheel.
Then you can replace all of the places where you have:
digitalWrite(m1, HIGH);
digitalWrite(m2, LOW);
analogWrite(EN1, 255);
with a single line calling:
void SetMotor(byte pins[3], int IN1, int IN2, int ENA)
{
digitalWrite(pins[0], IN1);
digitalWrite(pins[1], IN2);
analogWrite(pins[2], ENA);
}
And you can write a StopMotor, Forward, and Backward that uses SetMotor().
Your stop() would then become:
void stop()
{
StopMotor(Motor1);
StopMotor(Motor2);
delay(1000);
}
That would be MUCH more readable and understandble.
Thank you sir @johnwasser for the advice i hopefully will follow the instruction given by you
With best regard from
Lucky
This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.