Last Programme
Last Programme
h>
int pos = 0;
void setup() {
// put your setup code here, to run once:
myservo.attach(11);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
Serial.begin(9600);
digitalWrite(6, HIGH);
}
void loop() {
// put your main code here, to run repeatedly:
int a = analogRead(A0);
int b = analogRead(A1);
int c = analogRead(A2);
Serial.print(a);
Serial.print(" ");
Serial.print(b);
Serial.print(" ");
Serial.print(c);
Serial.println(" ");
delay(50);
if (a<=250)
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
}
else if(c<=250)
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 60; pos <= 120; pos += 1) { // goes from 0 degrees to 180 degrees
// in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
for (pos = 120; pos >= 60; pos -= 1) { // goes from 180 degrees to 0 degrees
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15 ms for the servo to reach the position
}
{
digitalWrite(6, HIGH);
digitalWrite(2, LOW);
digitalWrite(3,HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(500);
digitalWrite(6, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(500);
digitalWrite(2, LOW);
digitalWrite(3,HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
delay(500);
digitalWrite(6, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
delay(500);
else
{
digitalWrite(6, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
Here's an improved version of your program with better organization, efficiency, and readability. The
improvements include modularizing repetitive code into reusable functions, removing unnecessary delays,
optimizing servo control, and adding meaningful comments for clarity.
Optimized Code
#include <Servo.h>
// Pin definitions
const int servoPin = 11;
const int ledPins[] = {2, 3, 4, 5}; // Array for LED pins
const int inputPins[] = {A0, A1, A2}; // Array for input pins
const int relayPin = 6;
void setup() {
// Initialize servo
myservo.attach(servoPin);
void loop() {
// Read analog inputs
int readings[3];
for (int i = 0; i < 3; i++) {
readings[i] = analogRead(inputPins[i]);
}
// Decision-making logic
if (readings[0] <= 250 || readings[2] <= 250) {
setLEDStates(ledOff);
controlRelay(LOW);
oscillateServo(60, 120, 15, 2); // Oscillate servo twice
}
else if (readings[0] >= 251 && readings[0] <= 700) {
controlRelay(HIGH);
setLEDStates(pattern1);
delay(500);
setLEDStates(pattern2);
delay(500);
}
else if (readings[1] >= 251 && readings[1] <= 800) {
setLEDStates(pattern1);
controlRelay(HIGH);
}
else if (readings[2] >= 251 && readings[2] <= 800) {
setLEDStates(pattern2);
controlRelay(HIGH);
delay(500);
}
else {
setLEDStates(pattern2);
controlRelay(HIGH);
}
}
Key Improvements:
1. Modularization: Repetitive tasks like setting LED states, controlling the relay, and servo oscillation are
extracted into reusable functions.
2. Efficiency: Removed redundant code and unnecessary delays for faster execution.
3. Readability: Organized the code with meaningful variable names, comments, and consistent formatting.
4. Scalability: Easier to add or modify functionality (e.g., new LED patterns or input logic).
5. Debugging: Serial output includes labels for clarity.
Let me know if you'd like further customizations!