Created
August 20, 2015 04:54
-
-
Save JayBeavers/62a0c72e95189c70d3d9 to your computer and use it in GitHub Desktop.
The Internet of Ice Buclet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define BRAKEVCC 0 | |
#define CW 1 | |
#define CCW 2 | |
#define BRAKEGND 3 | |
#define CS_THRESHOLD 100 | |
/* VNH2SP30 pin definitions | |
xxx[0] controls '1' outputs | |
xxx[1] controls '2' outputs */ | |
int inApin[2] = {7, 4}; // INA: Clockwise input | |
int inBpin[2] = {8, 9}; // INB: Counter-clockwise input | |
int pwmpin[2] = {5, 6}; // PWM input | |
int cspin[2] = {2, 3}; // CS: Current sense ANALOG input | |
int enpin[2] = {0, 1}; // EN: Status of switches output (Analog pin) | |
int statpin = 13; | |
void setup() | |
{ | |
Serial1.begin(9600); | |
pinMode(statpin, OUTPUT); | |
// Initialize digital pins as outputs | |
for (int i=0; i<2; i++) | |
{ | |
pinMode(inApin[i], OUTPUT); | |
pinMode(inBpin[i], OUTPUT); | |
pinMode(pwmpin[i], OUTPUT); | |
} | |
// Initialize braked | |
for (int i=0; i<2; i++) | |
{ | |
digitalWrite(inApin[i], LOW); | |
digitalWrite(inBpin[i], LOW); | |
} | |
} | |
bool lights = false; | |
bool lit = false; | |
bool horn = false; | |
bool honk = false; | |
void loop() | |
{ | |
if (Serial1.available()) { | |
char c = Serial1.read(); | |
if (c == 'f') { | |
motorGo(0, CW, 1023); | |
} | |
if (c == 'b') { | |
motorGo(0, CCW, 1023); | |
} | |
if (c == 'l') { | |
lights = !lights; | |
if (!lights) { | |
digitalWrite(10, LOW); | |
} | |
} | |
if (c == 'h') { | |
horn = !horn; | |
if (!horn) { | |
digitalWrite(11, LOW); | |
} | |
} | |
} | |
delay(200); | |
if (lights) { | |
lit = !lit; | |
if (lit) { | |
digitalWrite(10, HIGH); | |
} else { | |
digitalWrite(10, LOW); | |
} | |
} | |
if (horn) { | |
honk = !honk; | |
if (honk) { | |
digitalWrite(11, HIGH); | |
} else { | |
digitalWrite(11, LOW); | |
} | |
} | |
motorOff(0); | |
} | |
void motorOff(int motor) | |
{ | |
// Initialize braked | |
for (int i=0; i<2; i++) | |
{ | |
digitalWrite(inApin[i], LOW); | |
digitalWrite(inBpin[i], LOW); | |
} | |
analogWrite(pwmpin[motor], 0); | |
} | |
void motorGo(uint8_t motor, uint8_t direct, uint8_t pwm) | |
{ | |
if (motor <= 1) | |
{ | |
if (direct <=4) | |
{ | |
// Set inA[motor] | |
if (direct <=1) | |
digitalWrite(inApin[motor], HIGH); | |
else | |
digitalWrite(inApin[motor], LOW); | |
// Set inB[motor] | |
if ((direct==0)||(direct==2)) | |
digitalWrite(inBpin[motor], HIGH); | |
else | |
digitalWrite(inBpin[motor], LOW); | |
analogWrite(pwmpin[motor], pwm); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment