Straight to the point: Can you give me pointers on how to make this code more maintainable? In the future, I want to add more more things to this, but first it should be easy to maintain/read. Should making a library be considered?
What it does:
Takes input from the serial line and converts it to servo commands + all the bells and whistles added.
#include <NewPing.h>
#include <Servo.h>
#define errorLED 13
#define ThrottlePin 2
#define RollPin 3
#define PitchPin 5
#define YawPin 4
#define Aux1Pin 6
#define Trigger 18
#define Echo 19
#define HEADING_PIN 15
NewPing sonar(Trigger, Echo);
Servo Throttle;
Servo Roll;
Servo Pitch;
Servo Yaw;
Servo Aux1;
unsigned int throttle = DEFAULT_THROTTLE;
unsigned int roll = DEFAULT_ROLL;
unsigned int pitch = DEFAULT_PITCH;
unsigned int yaw = DEFAULT_YAW;
unsigned int aux1 = DEFAULT_AUX1;
unsigned int temp = 0;
unsigned int range = 7;
unsigned long lastSerialData = 0;
unsigned long lastThrottleUpdate = 0;
unsigned long ledPreviousMillis = 0;
const int DEFAULT_THROTTLE = 45;
const int DEFAULT_ROLL = 70;
const int DEFAULT_PITCH = 70;
const int DEFAULT_YAW = 70;
const int DEFAULT_AUX1 = 45;
boolean isLedOn = false;
//Pins 7-12 power
//A0-A3 pin 13-16
boolean index = 7;
boolean circleIndex = 14;
void setup()
{
Throttle.attach(ThrottlePin);
Roll.attach(RollPin);
Pitch.attach(PitchPin);
Yaw.attach(YawPin);
Aux1.attach(Aux1Pin);
Serial.begin(115200);
for (byte x = 7; x <= 12; x++)
pinMode(x, OUTPUT);
for (byte xy = 14; xy <= 17; xy++)
pinMode(xy, OUTPUT);
setPowerPinsOn(false);
setGroundPinsOn(true);
pinMode(errorLED, OUTPUT);
}
int GetFromSerial()
{
// wait until we have some serial data
while (Serial.available() == 0) {
if (millis() - lastSerialData > 1000) {
digitalWrite(errorLED, HIGH);
readSonar();
blinkingLed();
autoLand();
}
}
lastSerialData = millis();
digitalWrite(errorLED, LOW);
return Serial.read();
}
void loop()
{
switch (GetFromSerial()) {
case 't':
temp = 0;
temp = GetFromSerial() + 45;
if (temp >= 45 && temp <= 141)
throttle = temp; //45 to 141
Throttle.write(throttle);
break;
case 'r':
temp = 0;
temp = GetFromSerial() + 45;
if (temp >= 45 && temp <= 141)
roll = map(temp, 45, 141, 69, 117); //45 to 141
if (roll < (93 + range) && roll > (93 - range))
roll = 93;
Roll.write(roll);
break;
case 'p':
temp = 0;
temp = GetFromSerial() + 45;
if (temp >= 45 && temp <= 141)
pitch = map(temp, 45, 141, 69, 117); //45 to 141
if (pitch < (93 + range) && pitch > (93 - range))
pitch = 93;
Pitch.write(pitch);
break;
case 'y':
temp = 0;
temp = GetFromSerial() + 45;
if (temp >= 45 && temp <= 141)
yaw = map(temp, 45, 141, 68, 117); //45 to 141
Yaw.write(yaw);
break;
case 'a':
temp = 0;
temp = GetFromSerial() + 45;
if (temp >= 45 && temp <= 141)
aux1 = temp; //45 to 141
Aux1.write(aux1);
break;
} // end switch
if (throttle <= 45) //Connected but not flying
circleLed();
else if (throttle >= 45 && aux1 > 45)
headingLed();
}
void autoLand()
{
if (throttle <= 60 && aux1 >= 50) {
throttle = 45;
aux1 = 45;
} else if (throttle >= 45)
if (millis() - lastThrottleUpdate > 400) {
throttle = throttle * .95;
aux1 = 45;
lastThrottleUpdate = millis();
}
writeAllValues();
}
void writeAllValues()
{
Throttle.write(throttle);
Roll.write(roll);
Pitch.write(pitch);
Yaw.write(yaw);
Aux1.write(aux1);
}
void setPowerPinsOn(boolean on)
{
if (on) {
for (byte x = 7; x <= 12; x++)
digitalWrite(x, HIGH);
} else
for (byte x = 7; x <= 12; x++)
digitalWrite(x, LOW);
}
void setGroundPinsOn(boolean on)
{
if (on) {
for (byte x = 14; x <= 17; x++)
digitalWrite(x, LOW);
} else
for (byte x = 14; x <= 17; x++)
digitalWrite(x, HIGH);
}
void circleLed()
{
if (millis() - ledPreviousMillis > 400) {
if (circleIndex == 18)
circleIndex = 14;
ledPreviousMillis = millis();
setPowerPinsOn(true);
setGroundPinsOn(false);
digitalWrite(circleIndex, LOW);
circleIndex++;
}
}
void blinkingLed()
{
if (millis() - ledPreviousMillis > 1000) {
ledPreviousMillis = millis();
isLedOn = !isLedOn;
setPowerPinsOn(isLedOn);
setGroundPinsOn(true);
}
}
void headingLed()
{
if (millis() - ledPreviousMillis > 300 - (throttle - 45) * 2) {
ledPreviousMillis = millis();
if (index == 13)
index = 7;
setPowerPinsOn(false);
setGroundPinsOn(false);
digitalWrite(HEADING_PIN, LOW);
digitalWrite(index, HIGH);
index++;
}
}
void readSonar()
{
int uS = sonar.ping_median();
Serial.println(uS / US_ROUNDTRIP_CM);
}
Summary: How can this code be more maintainable? Should making a library be considered?