0% found this document useful (0 votes)
12 views5 pages

Include - Servo.-Wps Office

Uploaded by

sarambaflora
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)
12 views5 pages

Include - Servo.-Wps Office

Uploaded by

sarambaflora
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/ 5

#include <Servo.

h>

// Define the servo motors

Servo servoUpDown; // For moving the crane up/down

Servo servoLeftRight; // For rotating the crane left/right

// Define push button pins

const int buttonUp = 2;

const int buttonDown = 3;

const int buttonLeft = 4;

const int buttonRight = 5;

// Define thermistor pin

const int thermistorPin = A0;

// Define motor driver pins (for rotation control)

const int motorPin1 = 6; // L298N IN1

const int motorPin2 = 7; // L298N IN2

const int motorPin3 = 8; // L298N IN3

const int motorPin4 = 11; // L298N IN4

int temperature = 0; // Variable to store thermistor value

int angleUpDown = 90; // Initial servo position for Up/Down (center position)

int angleLeftRight = 90; // Initial servo position for Left/Right (center position)
void setup() {

// Initialize the servos

servoUpDown.attach(9); // Servo 1 on Pin 9 (Up/Down)

servoLeftRight.attach(10); // Servo 2 on Pin 10 (Left/Right)

// Initialize the push buttons as inputs with internal pull-up resistors

pinMode(buttonUp, INPUT_PULLUP);

pinMode(buttonDown, INPUT_PULLUP);

pinMode(buttonLeft, INPUT_PULLUP);

pinMode(buttonRight, INPUT_PULLUP);

// Initialize motor driver pins as outputs

pinMode(motorPin1, OUTPUT);

pinMode(motorPin2, OUTPUT);

pinMode(motorPin3, OUTPUT);

pinMode(motorPin4, OUTPUT);

// Start with servo motors in the center position

servoUpDown.write(angleUpDown);

servoLeftRight.write(angleLeftRight);

void loop() {

// Read the push button states

int upState = digitalRead(buttonUp);


int downState = digitalRead(buttonDown);

int leftState = digitalRead(buttonLeft);

int rightState = digitalRead(buttonRight);

// Move crane up

if (upState == LOW) {

angleUpDown += 5; // Increment the angle for Up movement

if (angleUpDown > 180) angleUpDown = 180; // Limit to max 180°

servoUpDown.write(angleUpDown);

delay(200); // Small delay to avoid button bounce

// Move crane down

if (downState == LOW) {

angleUpDown -= 5; // Decrement the angle for Down movement

if (angleUpDown < 0) angleUpDown = 0; // Limit to min 0°

servoUpDown.write(angleUpDown);

delay(200); // Small delay to avoid button bounce

// Rotate crane left

if (leftState == LOW) {

angleLeftRight -= 5; // Decrement the angle for Left rotation

if (angleLeftRight < 0) angleLeftRight = 0; // Limit to min 0°

servoLeftRight.write(angleLeftRight);
digitalWrite(motorPin1, HIGH); // Rotate motor left (clockwise)

digitalWrite(motorPin2, LOW);

delay(200); // Small delay to avoid button bounce

// Rotate crane right

if (rightState == LOW) {

angleLeftRight += 5; // Increment the angle for Right rotation

if (angleLeftRight > 180) angleLeftRight = 180; // Limit to max 180°

servoLeftRight.write(angleLeftRight);

digitalWrite(motorPin3, HIGH); // Rotate motor right (counter-clockwise)

digitalWrite(motorPin4, LOW);

delay(200); // Small delay to avoid button bounce

// Read temperature from thermistor (optional feature)

temperature = analogRead(thermistorPin);

float voltage = temperature * 5.0 / 1024.0;

float tempCelsius = (voltage - 0.5) * 100.0; // Convert to Celsius (based on thermistor calibration)

// Print temperature to serial monitor (optional)

Serial.begin(9600);

Serial.print("Temperature: ");

Serial.print(tempCelsius);

Serial.println(" C");
delay(100); // Small delay for stability

You might also like