Skip to content

Instantly share code, notes, and snippets.

@nicklargent
Created May 7, 2023 03:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicklargent/a490d8c4fa75e4dd797426dc4bbe56ee to your computer and use it in GitHub Desktop.
Save nicklargent/a490d8c4fa75e4dd797426dc4bbe56ee to your computer and use it in GitHub Desktop.
DoorBot Arduino Code
/*
* Learn how to use the OSEPP motor shield. It can drive 2 servos plus 4 dc motors or 2 steppers.
*
* I'm attempting to hook up one MG995 servo and one geared dc motor. The ones I intend to use for my doorbot.
*/
#include <AFMotor.h>
#include <Servo.h>
#include <IRremote.hpp>
#define IR_RECV_PIN 13
#define SERVO_PWM_PIN 9
Servo servo1;
AF_DCMotor motor(3);
void setup() {
IrReceiver.begin(IR_RECV_PIN, true); // Start the receiver
motor.setSpeed(255);
motor.run(RELEASE);
raiseWheel();
Serial.begin(9600);
}
int servoPwmValue = 90;
void lowerWheel() {
servo1.attach(SERVO_PWM_PIN);
while (servoPwmValue < 140) {
servoPwmValue += 1;
servo1.write(servoPwmValue);
delay(10);
}
servo1.detach();
}
void raiseWheel() {
servo1.attach(SERVO_PWM_PIN);
while (servoPwmValue > 18) {
servoPwmValue -= 1;
servo1.write(servoPwmValue);
delay(10);
}
servo1.detach();
}
void startOpen() {
lowerWheel();
delay(100);
motor.run(FORWARD);
}
void startClose() {
lowerWheel();
delay(100);
motor.run(BACKWARD);
}
void stopMove() {
motor.run(RELEASE);
delay(100);
raiseWheel();
}
enum MODE {
IDLE,
OPENING,
CLOSING
};
MODE mode = IDLE;
int stopCountdown = 0;
#define COUNTDOWN 50
void loop() {
if (IrReceiver.decode()) {
int cmd = IrReceiver.decodedIRData.command;
Serial.println(cmd);
switch (mode) {
case IDLE:
switch (cmd) {
case 66: // UP
mode = OPENING;
stopCountdown = COUNTDOWN;
Serial.println("startOpen");
startOpen();
break;
case 67: // DOWN
mode = CLOSING;
stopCountdown = COUNTDOWN;
Serial.println("startClose");
startClose();
break;
}
break;
default:
stopCountdown = COUNTDOWN;
}
IrReceiver.resume(); // Enable receiving of the next value
}
//Serial.print("ct: ");Serial.println(stopCountdown);
if (stopCountdown > 0 && mode != IDLE) {
stopCountdown --;
if (stopCountdown == 0) {
mode = IDLE;
Serial.println("stop");
stopMove();
}
}
delay(3);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment