hello,
I am sure this is an age old question, however, I am having problems with lag in my servos when I try to add programming to the main program loop. Is it possible I am asking to much from the arduino or is there a fix for this?
See code:
// =============== Loads Libraries =========================
#include <Wire.h>
#include "I2Cdev.h"
#include <BMP085.h>
#include "HMC5883L.h"
#include <Servo.h>
#include <LiquidCrystal.h>
//================ Configure Hardware ======================
// ==== For Servo Control ======
Servo svr1;Servo svr2;Servo svr3;
Servo svr4;Servo svr5;Servo svr6;
int s1=90;int s2=90;int s3=90;
int s4=90;int s5=90;int s6=90;
// ==== for Serial input =======================
int index;int index_1;
String inputString = "";String outputString = "";
String data = "";String data_1 = "";
String temp_1="";String temp_2="";
boolean stringComplete = false;
//======== For BMP085 ===========================
BMP085 dps = BMP085();
long Pressure = 0, Altitude = 0;
unsigned long time1=0;
float ft = 0;
String alt = "100";
int other = 0;
int sensorValue;
// ====== Main Setup Function ================================
void setup(){
inputString.reserve(200);
outputString.reserve(200);
Serial.begin(115200);
delay(100);
svr1.attach(38);svr2.attach(39);svr3.attach(40);
svr4.attach(41);svr5.attach(42);svr6.attach(43);
// Used for testing only.
outputString = ("100.00,200.00,300.00,400.00,500.00,600.00");
//====== For BMP-085 ========================================
dps.init(MODE_STANDARD, 24170, true); //Altitude at home.
}
// ===== Main Program Loop ====================================
void loop(){
//===== Write data to Servos ===============
svr1.write(s1);svr2.write(s2);svr3.write(s3);
svr4.write(s4);svr5.write(s5);svr6.write(s6);
// When I add this the program slows to a crawl and servos respond slowly.
/*
getAlt();
*/
}
// =============== End of Loop ==========================
void serialEvent(){
while (Serial.available()) {
char inChar = (char)Serial.read();
inputString += inChar;
if (inChar == '\n') {
stringComplete = true;
}
}
if (stringComplete) {
data = inputString.substring(0, inputString.length() -1);
index = data.indexOf(",");
temp_1 = data.substring(0, index);
// Sends data to servo 1
s1 = temp_1.toInt();
data_1 = data.substring(index+1, data.length());
index_1 = data_1.indexOf(",");
temp_2 = data.substring(index +1, index+index_1+1);
s2 = temp_2.toInt();
data = data_1.substring(index_1+1, data.length());
index = data.indexOf(",");
temp_1 = data.substring(0, index);
s3 = temp_1.toInt();
data_1 = data.substring(index+1, data.length());
index_1 = data_1.indexOf(",");
temp_2 = data.substring(index +1, index+index_1+1);
s4 = temp_2.toInt();
data = data_1.substring(index_1+1, data.length());
index = data.indexOf(",");
temp_1 = data.substring(0, index);
s5 = temp_1.toInt();
data_1 = data.substring(index+1, data.length());
index_1 = data_1.indexOf(",");
temp_2 = data.substring(index +1, index+index_1+1);
s6 = temp_2.toInt();
// clear the string:
inputString = "";
stringComplete = false;
}
Serial.println(outputString);
}
void getAlt() {
if (((millis() - time1)/1000.0) >= 1.0) {
dps.calcTrueTemperature();
time1 = millis();
}
dps.getPressure(&Pressure);
dps.getAltitude(&Altitude);
ft = Altitude / 30.4;
}