Skip to content

Instantly share code, notes, and snippets.

@mithi
Created December 7, 2015 08:32
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 mithi/0b35252d97db67f8b465 to your computer and use it in GitHub Desktop.
Save mithi/0b35252d97db67f8b465 to your computer and use it in GitHub Desktop.
Ringo OOP
#include "RingoSimpleLibrary.h"
Motors motors;
Communication communication;
void setup() {
}
void loop() {
motors.Drive(255, 255);
delay(1000);
communication.Enable();
Serial.println("hello World!");
communication.Disable();
}
void initialize(){
motors.Enable();
}
#include "Arduino.h"
/********************************************
* PINS
********************************************/
#define PIN_MOTOR_CAP_BATT_VOLTS 0 //AD0 if SOURCE_SELECT == LOW, then MOTOR CAPACITOR enabled else BATTERY
#define PIN_SERIAL_RECEIVE 0
#define PIN_LEFT_MOTOR_DIRECTION 0
#define PIN_RIGHT_MOTOR_DIRECTION 1
#define PIN_SOURCE_SELECT 4
#define PIN_RIGHT_MOTOR_DRIVE 5
#define PIN_LEFT_MOTOR_DRIVE 6
/********************************************
* OTHERS
********************************************/
#define SERIAL_SPEED 57600
#define MAX_MOTOR_SPEED 255
#define MOTOR_REVERSE 0
#define MOTOR_FORWARD 1
/********************************************
* MODES
********************************************/
extern int motor_ready;
extern int print_ready;
class Motors{
public:
void Enable();
void Drive(int left_speed, int right_speed);
void DriveWheel(int speed, int direction_pin, int drive_pin);
void Stop();
};
class Communication{
public:
void Enable();
void Disable();
};
#include "RingoSimpleLibrary.h"
int motor_ready;
int print_ready;
void Motors::Enable(){
pinMode(PIN_RIGHT_MOTOR_DIRECTION, OUTPUT);
pinMode(PIN_LEFT_MOTOR_DIRECTION, OUTPUT);
motor_ready = 1;
print_ready = 0;
}
void Motors::Drive(int left_speed, int right_speed){
if(!motor_ready)
Enable();
DriveWheel(left_speed, PIN_LEFT_MOTOR_DIRECTION, PIN_LEFT_MOTOR_DRIVE);
DriveWheel(right_speed, PIN_RIGHT_MOTOR_DIRECTION, PIN_RIGHT_MOTOR_DRIVE);
}
void Motors::DriveWheel(int speed, int direction_pin, int drive_pin){
speed = constrain(speed, -MAX_MOTOR_SPEED, MAX_MOTOR_SPEED);
speed < 0 ? digitalWrite(direction_pin, MOTOR_REVERSE) : digitalWrite(direction_pin, MOTOR_FORWARD);
analogWrite(drive_pin, abs(speed));
}
void Motors::Stop(){
Drive(0, 0);
}
void Communication::Enable(){
pinMode(PIN_SERIAL_RECEIVE, INPUT);
Serial.begin(SERIAL_SPEED);
motor_ready = 0;
print_ready = 1;
}
void Communication::Disable(){
Serial.end();
pinMode(PIN_LEFT_MOTOR_DIRECTION, OUTPUT);
pinMode(PIN_RIGHT_MOTOR_DIRECTION, OUTPUT);
motor_ready = 1;
print_ready = 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment