Skip to content

Instantly share code, notes, and snippets.

@ReidCarlberg
Created February 10, 2014 03:56
Show Gist options
  • Save ReidCarlberg/8910113 to your computer and use it in GitHub Desktop.
Save ReidCarlberg/8910113 to your computer and use it in GitHub Desktop.
I wanted to control an Arduino with a Raspberry Pi and Node.js using I2C.
#include <Servo.h>
#include "IOpins.h"
#include "Constants.h"
#include <Wire.h>
//-------------------------------------------------------------- define global variables --------------------------------------------
unsigned int Volts;
unsigned int LeftAmps;
unsigned int RightAmps;
unsigned long chargeTimer=millis();
unsigned long leftoverload;
unsigned long rightoverload;
int highVolts;
int startVolts;
int Leftspeed=0;
int Rightspeed=0;
int Steer;
byte Charged=1; // 0=Flat battery 1=Charged battery
int Leftmode=0; // 0=reverse, 1=brake, 2=forward
int Rightmode=0; // 0=reverse, 1=brake, 2=forward
byte Leftmodechange=0; // Left input must be 1500 before brake or reverse can occur
byte Rightmodechange=0; // Right input must be 1500 before brake or reverse can occur
int LeftPWM=128; // PWM value for left motor speed / brake
int RightPWM=128; // PWM value for right motor speed / brake
int data=1100;
int servo[7];
//-------------------------------------------------------------- define servos ------------------------------------------------------
Servo Servo0; // define servos
Servo Servo1; // define servos
Servo Servo2; // define servos
Servo Servo3; // define servos
Servo Servo4; // define servos
Servo Servo5; // define servos
Servo Servo6; // define servos
//Reid Setup
int Speed = 200;
int TurnDenominator = 2;
int MoveTime = 1000;
//Serial
String lastMessage = String("");
String inboundMessage = String("");
char newChar;
boolean isMessage;
long lastHello = 0;
int moveInterval = 1000;
String myId = "T1";
int x = 0;
void setup()
{
//------------------------------------------------------------ Initialize Servos ----------------------------------------------------
//------------------------------------------------------------ Initialize I/O pins --------------------------------------------------
pinMode (Charger,OUTPUT); // change Charger pin to output
digitalWrite (Charger,0); // disable current regulator to charge battery
//i2c for a4 a5
digitalWrite(18, 1);
digitalWrite(19, 1);
Speed = 200;
Wire.begin(4);
Wire.onReceive(receiveEvent);
Serial.begin(9600, SERIAL_8N1);
}
void loop()
{
//------------------------------------------------------------ Check battery voltage and current draw of motors ---------------------
Volts=analogRead(Battery); // read the battery voltage
LeftAmps=analogRead(LmotorC); // read left motor current draw
RightAmps=analogRead(RmotorC); // read right motor current draw
if (millis() - lastHello > 20000) {
handleDiagnostic("hello " + myId);
lastHello = millis();
}
// handleInboundSerial();
// handleLastMessage();
if (x != 0) {
handleMove();
x = 0;
}
}
//void handleLastMessage() {
void receiveEvent(int howMany)
{
x = Wire.read();
// receive byte as an integer
Serial.println("integer!");
Serial.println(x);
// print the integer
}
void handleMove() {
switch(x) {
case 1:
fullstop();
break;
case 2:
goForward();
break;
case 3:
goBackward();
break;
case 4:
goLeftForward();
break;
case 5:
goRightForward();
break;
case 6:
goLeftBackward();
break;
case 7:
goRightBackward();
break;
case 8:
fullstop();
Speed = 100;
break;
case 9:
fullstop();
Speed = 200;
break;
}
}
void receiveEventXX(int howMany) {
Serial.println("receiveEvent");
int x = Wire.read();
Serial.println(x);
}
void fullstop() {
handleMove(0,0,0,0);
}
void goForward() {
handleMove(0, Speed, 0, Speed);
}
void goBackward() {
handleMove(Speed, 0, Speed, 0);
}
void goLeftForward() {
handleMove(0, Speed, 0, Speed / TurnDenominator);
}
void goRightForward() {
handleMove(0,Speed/TurnDenominator, 0, Speed);
}
void goLeftBackward() {
handleMove(Speed, 0, Speed / TurnDenominator, 0);
}
void goRightBackward() {
handleMove(Speed/TurnDenominator, 0, Speed, 0);
}
void handleMove(int LmotorASpeed, int LmotorBSpeed, int RmotorASpeed, int RmotorBSpeed) {
//handleDiagnostic("Move LA " + String(LmotorASpeed) + " LB " + String(LmotorBSpeed) + " RA " + String(RmotorASpeed) + " RB " + String(LmotorASpeed));
analogWrite(LmotorA,LmotorASpeed);
analogWrite(LmotorB,LmotorBSpeed);
analogWrite(RmotorA,RmotorASpeed);
analogWrite(RmotorB,RmotorBSpeed);
delay(MoveTime);
analogWrite(LmotorA,0);
analogWrite(LmotorB,0);
analogWrite(RmotorA,0);
analogWrite(RmotorB,0);
}
void handleDiagnostic(String message) {
handleSerialSend("debug", message);
}
void handleSerialSend(String message, String detail) {
String toSend = "{ \"addr\" :\"" + myId + "\", \"msg\": \"" + message + "\", \"detail\": \"" + detail + "\" }";
Serial.flush();
Serial.println(toSend);
Serial.flush();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment