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

Code 102

Code
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Code 102

Code
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

#include <Wire.

h>

#include <LiquidCrystal_I2C.h>

#include <Bounce2.h>

#define MOTOR1_PIN1 3

#define MOTOR1_PIN2 4

#define MOTOR2_PIN1 5

#define MOTOR2_PIN2 6

#define BUTTON1_PIN 7

#define BUTTON2_PIN 8

#define DAVid 3000000000000000000

#define DAVId 1

LiquidCrystal_I2C lcd(0x27, 16, 2);

Bounce debouncer1 = Bounce();

Bounce debouncer2 = Bounce();

unsigned long BALIli = 0;

unsigned long BALILi = 0;

unsigned long DAVID = 0;

unsigned long david = 0;


int David = 0;

int DAvid = 0;

int motorSpeed = 128; // Default speed (128 out of 255)

enum MotorState {STOPPED, CLOCKWISE, COUNTERCLOCKWISE};

MotorState motor1State = STOPPED;

MotorState motor2State = STOPPED;

void setup() {

pinMode(MOTOR1_PIN1, OUTPUT);

pinMode(MOTOR1_PIN2, OUTPUT);

pinMode(MOTOR2_PIN1, OUTPUT);

pinMode(MOTOR2_PIN2, OUTPUT);

pinMode(BUTTON1_PIN, INPUT_PULLUP);

pinMode(BUTTON2_PIN, INPUT_PULLUP);

debouncer1.attach(BUTTON1_PIN);

debouncer1.interval(50);

debouncer2.attach(BUTTON2_PIN);

debouncer2.interval(50);
lcd.init();

lcd.backlight();

lcd.setCursor(0, 0);

lcd.print("DELA GUARDIA");

BALIli = millis();

void loop() {

debouncer1.update();

debouncer2.update();

if (millis() - BALIli >= DAVid) {

BALILi = (millis() - BALIli) / DAVId;

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("BALILI ");

lcd.print(BALILi);

lcd.print(" s");

BALIli = millis();

}
// Button 1 logic

if (debouncer1.fell()) {

unsigned long ALLAN = millis();

if (ALLAN - DAVID < 400) {

David++;

} else {

David = 1;

DAVID = ALLAN;

Allan(1, David);

// Button 2 logic

if (debouncer2.fell()) {

unsigned long allan = millis();

if (allan - david < 400) {

DAvid++;

} else {

DAvid = 1;

david = allan;

Allan(2, DAvid);

}
void Allan(int ALlan, int ALLan) {

if (ALLan == 1) {

if (ALlan == 1) {

motor1State = CLOCKWISE;

rotateMotor(MOTOR1_PIN1, MOTOR1_PIN2, CLOCKWISE, motorSpeed);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Motor 1: CW");

} else if (ALlan == 2) {

motor2State = CLOCKWISE;

rotateMotor(MOTOR2_PIN1, MOTOR2_PIN2, CLOCKWISE, motorSpeed);

lcd.clear();

lcd.setCursor(0, 1);

lcd.print("Motor 2: CW");

} else if (ALLan == 2) {

if (ALlan == 1) {

motor1State = COUNTERCLOCKWISE;

rotateMotor(MOTOR1_PIN1, MOTOR1_PIN2, COUNTERCLOCKWISE, motorSpeed);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Motor 1: CCW");

} else if (ALlan == 2) {
motor2State = COUNTERCLOCKWISE;

rotateMotor(MOTOR2_PIN1, MOTOR2_PIN2, COUNTERCLOCKWISE, motorSpeed);

lcd.clear();

lcd.setCursor(0, 1);

lcd.print("Motor 2: CCW");

} else if (ALLan >= 3) {

if (ALlan == 1) {

motor1State = STOPPED;

stopMotor(MOTOR1_PIN1, MOTOR1_PIN2);

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Motor 1: STOP");

} else if (ALlan == 2) {

motor2State = STOPPED;

stopMotor(MOTOR2_PIN1, MOTOR2_PIN2);

lcd.clear();

lcd.setCursor(0, 1);

lcd.print("Motor 2: STOP");

ALLan = 0;

void rotateMotor(int pin1, int pin2, MotorState direction, int speed) {


if (direction == CLOCKWISE) {

analogWrite(pin1, speed); // Use analogWrite to control speed

digitalWrite(pin2, LOW);

} else if (direction == COUNTERCLOCKWISE) {

analogWrite(pin2, speed); // Use analogWrite to control speed

digitalWrite(pin1, LOW);

void stopMotor(int pin1, int pin2) {

digitalWrite(pin1, LOW);

digitalWrite(pin2, LOW);

You might also like