Created
May 20, 2014 22:05
-
-
Save brandoaire/1c7a175d96c269283e8e to your computer and use it in GitHub Desktop.
Alternative firmware for the RC Car that features local communication through TCP, and speed control for driving and turning
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
/* Globals -------------------------------------------------------------------*/ | |
int leftMotorEnable = D1; //PWM | |
int rightMotorEnable = A7; //PWM | |
int leftMotorDir = D3; | |
int rightMotorDir = D4; | |
int dance_state = 0; | |
int speed_f = 0; | |
int speed_b = 0; | |
int speed_l = 0; | |
int speed_r = 0; | |
int cloud_state = 1; | |
TCPServer server = TCPServer(80); | |
TCPClient client; | |
char myIpString[24]; | |
void setup() { | |
server.begin(); | |
Serial.begin(9600); | |
delay(1000); | |
//I expose Ip Address of the core so I can learn it from cloud and connect it with socket on my pc. | |
IPAddress myIp = Network.localIP(); | |
sprintf(myIpString, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]); | |
Spark.variable("ipAddress", myIpString, STRING); | |
pinMode(leftMotorDir, OUTPUT); | |
pinMode(leftMotorEnable, OUTPUT); | |
pinMode(rightMotorDir, OUTPUT); | |
pinMode(rightMotorEnable, OUTPUT); | |
pinMode(D7,OUTPUT); | |
} | |
void loop() { | |
if (WiFi.status() != WIFI_ON) { | |
rcCarControl(byte('k')); | |
} | |
static system_tick_t last_announced_at = millis(); | |
if (millis() - last_announced_at > 10000) { | |
Multicast_Presence_Announcement(); | |
last_announced_at = millis(); | |
} | |
if (client.connected()) { | |
digitalWrite(D7,HIGH); | |
while (client.available()) { | |
rcCarControl(byte(client.read())); | |
} | |
} else { | |
// if no client is yet connected, check for a new connection | |
client = server.available(); | |
digitalWrite(D7,LOW); | |
} | |
if(dance_state == 1) { | |
int i=0; | |
int j=0; | |
digitalWrite(leftMotorDir, LOW); | |
digitalWrite(rightMotorDir, LOW); | |
for (i = 0; i<256; i++) { | |
analogWrite(leftMotorEnable,i); | |
analogWrite(rightMotorEnable,i); | |
delay(2); | |
} | |
for (j = 0; i<256; j++) { | |
analogWrite(leftMotorEnable,255-j); | |
analogWrite(rightMotorEnable,255-j); | |
delay(2); | |
} | |
} | |
} | |
void rcCarControl(byte cmd) { | |
if(cmd == byte('k')) | |
{ | |
digitalWrite(leftMotorEnable,LOW); | |
digitalWrite(rightMotorEnable,LOW); | |
digitalWrite(leftMotorDir,LOW); | |
digitalWrite(rightMotorDir,LOW); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Stopping"); | |
return; | |
} | |
if(cmd == byte('s')) | |
{ | |
if (speed_b == 0) { | |
analogWrite(leftMotorEnable,85); | |
analogWrite(rightMotorEnable,85); | |
digitalWrite(leftMotorDir,LOW); | |
digitalWrite(rightMotorDir,HIGH); | |
speed_f = 0; | |
speed_b = 1; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Backward - 1"); | |
return; | |
} | |
if (speed_b == 1) { | |
analogWrite(leftMotorEnable,170); | |
analogWrite(rightMotorEnable,170); | |
digitalWrite(leftMotorDir,LOW); | |
digitalWrite(rightMotorDir,HIGH); | |
speed_f = 0; | |
speed_b = 2; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Backward - 2"); | |
return; | |
} | |
else { | |
analogWrite(leftMotorEnable,255); | |
analogWrite(rightMotorEnable,255); | |
digitalWrite(leftMotorDir,LOW); | |
digitalWrite(rightMotorDir,HIGH); | |
speed_f = 0; | |
speed_b = 3; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Backward - 3"); | |
return; | |
} | |
} | |
if(cmd == byte('w')) { | |
if (speed_f == 0) { | |
analogWrite(leftMotorEnable,85); | |
analogWrite(rightMotorEnable,85); | |
digitalWrite(leftMotorDir, HIGH); | |
digitalWrite(rightMotorDir, LOW); | |
speed_f = 1; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Forward - 1"); | |
return; | |
} | |
if (speed_f == 1) { | |
analogWrite(leftMotorEnable,170); | |
analogWrite(rightMotorEnable,170); | |
digitalWrite(leftMotorDir, HIGH); | |
digitalWrite(rightMotorDir, LOW); | |
speed_f = 2; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Forward - 2"); | |
return; | |
} | |
else { | |
analogWrite(leftMotorEnable,255); | |
analogWrite(rightMotorEnable,255); | |
digitalWrite(leftMotorDir, HIGH); | |
digitalWrite(rightMotorDir, LOW); | |
speed_f = 3; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Forward - 3"); | |
return; | |
} | |
} | |
if(cmd == byte('d')) | |
{ | |
if (speed_r == 0) { | |
analogWrite(leftMotorEnable,85); | |
analogWrite(rightMotorEnable,85); | |
digitalWrite(leftMotorDir, HIGH); | |
digitalWrite(rightMotorDir, HIGH); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 1; | |
dance_state = 0; | |
server.write("Right - 1"); | |
return; | |
} | |
if (speed_r == 1) { | |
analogWrite(leftMotorEnable,170); | |
analogWrite(rightMotorEnable,170); | |
digitalWrite(leftMotorDir, HIGH); | |
digitalWrite(rightMotorDir, HIGH); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 2; | |
dance_state = 0; | |
server.write("Right - 2"); | |
return; | |
} | |
else { | |
analogWrite(leftMotorEnable,255); | |
analogWrite(rightMotorEnable,255); | |
digitalWrite(leftMotorDir, HIGH); | |
digitalWrite(rightMotorDir, HIGH); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 0; | |
speed_r = 3; | |
dance_state = 0; | |
server.write("Right - 3"); | |
return; | |
} | |
} | |
if(cmd == byte('a')) | |
{ | |
if (speed_l == 0) { | |
analogWrite(leftMotorEnable,85); | |
analogWrite(rightMotorEnable,85); | |
digitalWrite(leftMotorDir, LOW); | |
digitalWrite(rightMotorDir, LOW); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 1; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Left - 1"); | |
return; | |
} | |
if (speed_l == 1) { | |
analogWrite(leftMotorEnable,170); | |
analogWrite(rightMotorEnable,170); | |
digitalWrite(leftMotorDir, LOW); | |
digitalWrite(rightMotorDir, LOW); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 2; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Left - 2"); | |
return; | |
} | |
else { | |
analogWrite(leftMotorEnable,255); | |
analogWrite(rightMotorEnable,255); | |
digitalWrite(leftMotorDir, LOW); | |
digitalWrite(rightMotorDir, LOW); | |
speed_f = 0; | |
speed_b = 0; | |
speed_l = 3; | |
speed_r = 0; | |
dance_state = 0; | |
server.write("Left - 3"); | |
return; | |
} | |
} | |
if(cmd == byte('l')) { | |
dance_state = 1; | |
server.write("Dancing!"); | |
return; | |
} | |
if(cmd == byte('x')) { | |
if (cloud_state == 1) { | |
Spark.disconnect(); | |
cloud_state=0; | |
server.write("Disconnecting from Spark Cloud"); | |
return; | |
} | |
if (cloud_state == 0) { | |
Spark.connect(); | |
cloud_state=1; | |
server.write("Connecting to the Spark Cloud"); | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment