Skip to content

Instantly share code, notes, and snippets.

@jamespfinn
Created February 27, 2015 03:59
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 jamespfinn/3ec71596471edbc6a2a6 to your computer and use it in GitHub Desktop.
Save jamespfinn/3ec71596471edbc6a2a6 to your computer and use it in GitHub Desktop.
/*
02.26.2015 - jfinn -
We will drive the 24V DC motor w/ an H-bridge composed of 4 2n2222 transistors.
+12V --------+------------------------+
| |
PIN 1k / c c \ 1k PIN
13 --/\/\/---| 2N2222 2N2222 |--/\/\/--- 11
\> e e </
| + - |
|-------- MOTOR ---------|
| |
PIN 1k / c c \ 1k PIN
12 --/\/\/---| 2N2222 2N2222 |--/\/\/--- 10
\> e e </
| |
--- ---
For the below, 1=HIGH, 0=LOW
Making Pin 13 = 1, Pin 12 = 0, Pin 11 = 0, Pin 10 = 1 the motor would go forwards
Making Pin 13 = 0, Pin 12 = 1, Pin 11 = 1, Pin 10 = 0 the motor would go backwards
Making Pin 13 = 0, Pin 12 = 0, Pin 11 = 0, Pin 10 = 0 the motor coasts
Making Pin 13 = 1, Pin 12 = 0, Pin 11 = 1, Pin 10 = 0 the motor would brake (slow down quickly)
Making Pin 13 = 0, Pin 12 = 1, Pin 11 = 0, Pin 10 = 1 the motor would also brak
t1 = 13
t2 = 12
t3 = 11
t4 = 10
FULL PINOUT for ARDUINO UNO w/ ATMEGA328
DIGITAL
00
01
02 - TX to LCD Display
03 - Reserved for unused LCD RX
04 - PLANNED - IR Receiver 1 (ir1)
05 - PLANNED - IR Receiver 2 (ir2)
06 - REVERSE Button (sw2)
07 - FORWARD Button (sw1)
08
09 - PLANNED - Electromagnetic Break
10 - Base of transistor 4 (t4)
11 - Base of transistor 3 (t3)
12 - Base of transistor 2 (t2)
13 - Base of transistor 1 (t1)
*/
//need this for the LCD display
#include <SoftwareSerial.h>
// These contants are used by the 'drive' funtion for directionality.
#define STOP 0
#define FORWARD 1
#define REVERSE 2
int t1 = 13;
int t2 = 12;
int t3 = 11;
int t4 = 10;
int sw1 = 7; // FORWARD SWITCH
int sw2 = 6; // REVERSE SWITCH
int sw1val, sw2val; // These will hold our button values in the loop.
SoftwareSerial lcd(3,2); //occupy digital pin 2 & 3 for the LCD display.
//pin 3 is unused since we dont rx, only tx on 2.
char * last_msg = ""; // This will prevent us from outputting too much data.
// the setup routine runs once when you press reset:
void setup() {
// initialize the LCD display
lcd.begin(9600);
delay(500); // wait for lcd to boot
screen_write("Initializing....");
//initialize serial output
Serial.begin(9600);
Serial.println("Initializing inputs and outputs...");
// pause for dramatic effect.
delay(2000);
// initialize the digital pins as outputs
pinMode(t1, OUTPUT);
pinMode(t2, OUTPUT);
pinMode(t3, OUTPUT);
pinMode(t4, OUTPUT);
pinMode(sw1, INPUT);
pinMode(sw2, INPUT);
//being verbose..
Serial.println("initialized.");
screen_write("initialized.");
//ensure we are at a STOP
drive(STOP);
}
// the loop routine runs over and over again forever:
void loop() {
sw1val = digitalRead(sw1); // Check our FORWARD switch
sw2val = digitalRead(sw2); // Check our REVERSE switch
// Lets check the state of our inputs & act accordingly...
if(sw1val == HIGH) {
drive(FORWARD);
}
else if(sw2val == HIGH){
drive(REVERSE);
}
else
drive(STOP);
}
void screen_write(const char *msg){ // This function will write the first 16 chars
//of the msg to the LCD display
lcd.write(254); // move cursor to start of 1st line
lcd.write(128);
lcd.write(" "); // clear display
lcd.write(" ");
lcd.write(254); // move cursor to start of 1st line
lcd.write(128);
lcd.write("f3 StairLift 1.0");
lcd.write(msg);
}
// This function manipulates the H-brige transistors to move the motor
void drive(int command) {
char * msg;
switch(command) {
case FORWARD:
msg = "Chair FORWARD";
if(msg != last_msg){
Serial.println(msg);
screen_write(msg);
last_msg = msg;
}
digitalWrite(t1, HIGH);
digitalWrite(t4, HIGH);
Serial.println("driving forward");
break;
case REVERSE:
msg = "Chair REVERSE";
if(msg != last_msg){
Serial.println(msg);
screen_write(msg);
last_msg = msg;
}
digitalWrite(t2, HIGH);
digitalWrite(t3, HIGH);
Serial.println("driving reverse");
break;
case STOP:
msg = "Chair Stop&Brake";
if(msg != last_msg){
Serial.println(msg);
screen_write(msg);
last_msg = msg;
}
digitalWrite(t1, LOW);
digitalWrite(t2, LOW);
digitalWrite(t3, LOW);
digitalWrite(t4, LOW);
//TODO: Insert logic to engage brake here.
break;
default:
Serial.write("defautl action invoked. This shouldn't happen. Being safe and calling STOP!\n");
drive(STOP);
break;
}
}
void brake() { // This function will activate the electromagnetic brake attached to the motor
// TODO: make this or people could die.
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment