0% found this document useful (0 votes)
7 views

Last Programme

Uploaded by

abhijt Maitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Last Programme

Uploaded by

abhijt Maitra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <Servo.

h>

Servo myservo; // create servo object to control a servo


// twelve servo objects can be created on most boards

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
}

else if(a>=251 && a<=700)

{
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);

else if(b>=251 && b<=800)

digitalWrite(2, LOW);
digitalWrite(3,HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);

digitalWrite(6, HIGH);

else if(c>=251 && c<=800)

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>

Servo myservo; // Create servo object to control a servo

// 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;

int pos = 0; // Position variable for servo motor

void setup() {
// Initialize servo
myservo.attach(servoPin);

// Initialize LED and relay pins


for (int pin : ledPins) {
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); // Ensure LEDs are off initially
}
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH); // Default relay state

// Initialize input pins


for (int pin : inputPins) {
pinMode(pin, INPUT);
}

// Start serial communication


Serial.begin(9600);
}

// Function to perform servo oscillation


void oscillateServo(int startAngle, int endAngle, int delayMs, int repetitions) {
for (int i = 0; i < repetitions; i++) {
for (pos = startAngle; pos <= endAngle; pos++) {
myservo.write(pos);
delay(delayMs);
}
for (pos = endAngle; pos >= startAngle; pos--) {
myservo.write(pos);
delay(delayMs);
}
}
}

// Function to set LED states


void setLEDStates(bool states[]) {
for (int i = 0; i < 4; i++) {
digitalWrite(ledPins[i], states[i] ? HIGH : LOW);
}
}

// Function to handle relay state


void controlRelay(bool state) {
digitalWrite(relayPin, state ? HIGH : LOW);
}

void loop() {
// Read analog inputs
int readings[3];
for (int i = 0; i < 3; i++) {
readings[i] = analogRead(inputPins[i]);
}

// Print readings for debugging


Serial.print("A0: "); Serial.print(readings[0]);
Serial.print(" A1: "); Serial.print(readings[1]);
Serial.print(" A2: "); Serial.println(readings[2]);
delay(50);

// Define LED state arrays


bool ledOff[] = {LOW, LOW, LOW, LOW};
bool pattern1[] = {LOW, HIGH, HIGH, LOW};
bool pattern2[] = {HIGH, LOW, HIGH, LOW};

// 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!

You might also like