Hi
I am trying to combine three different sketches into one so that I can type of a couple of P controllers to run a small engine at a constant speed and the correct mixture settings using my arduino.
I have three sketches, one is a bases for controlling my two servos, one a regular servo the other a continuous serve. These will be responding to the results from the two sensors.
The two sensors are a MAX31855K thermocouple and a hall effect sensor that counts RPM.
Right now I am just trying to get the three pieces of code to run in the same sketch. Separated these sketches all work very well. When I try to compile things, I get a lot of errors about declared statements, etc.
Any advice on how to get these to run together would be great. Thanks.
Servo Control function (temporary) :
//Traxxas TRA2055 servo. 90 degrees of rotation. Values 50 thru 140. 95 is half. //Traxxas TRA2055 cont'd. For throttle only use - Throttle settings - Idle: 140 Wide Open: 95 (value not to be less than 95). Starting throttle position approx: 120 //LS-3006 continuous servo. Continuous rotation. For High Speed Needle use - Static: 92 Max lean: 180 Max rich: 0 Actual Lean: 99 (4.4s/turn) Actual Rich: 85 (4.4s/turn) Factory: four turns from closed //LS-3006 cont'd. angular speed at ideal: Lean: 5.3s/turn = 67.92 deg/sec for 30 deg (1 hour) turns, .4417 seconds (441.7 ms) for shared 5v power source //LS-3006 cont'd angular speed at ideal: Rich: 5.6s/turn = 64.29 deg/sec for 30 deg (1 hour) turns, .4667 seconds (466.7 ms) for shared 5v power source
include <Servo.h>
Servo traxxas; // create traxxas object to control the Throttle Servo LS; // create LS object ti control the HSN
//servo 1: Traxxas TRA2055
int Throttle; // variable throttle object
//servo 2: LS-3006
int HSN; //variable High Speed Needle position object
int N = 92; // create neutral position for LS-3006
int T = 110; // create variable throttle pos for TRA2055
void setup() { traxxas.attach(3); // attaches the Traxxas TRA2055 servo on pin 3 to the Throttle object
LS.attach(5); //attaches the LS-2006 servo on pin 5 to the HSN object }
void loop() {
Throttle = T; traxxas.write(Throttle); // sets the Traxxas TRA2055 servo position according to the Throttle value
delay(50);
HSN = 100; //pattern keeps the HSN moving
LS.write(HSN);
delay(466.7);
HSN = N;
LS.write(HSN);
delay(1000);
HSN = 80;
LS.write(HSN);
delay(466.7);
HSN = N;
LS.write(HSN);
delay(1000);
}
RPM sensor:
// read RPM
int half_revolutions = 0;
int rpm = 0;
unsigned long
lastmillis = 0;
void setup(){
Serial.begin(9600);
attachInterrupt(0, rpm_fan, FALLING); }
void loop(){
if (millis() - lastmillis == 1000){ //Uptade every one second, this will be equal to reading frecuency (Hz). detachInterrupt(0);//Disable interrupt when calculating
rpm = half_revolutions * 60; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use half_revolutions * 30. Serial.print("RPM =\t");
//print the word "RPM" and tab.
Serial.print(rpm); // print the rpm value.
Serial.print("\t Hz=\t"); //print the word "Hz".
Serial.println(half_revolutions);
//print revolutions per second or Hz. And print new line or enter.
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis();
// Uptade lasmillis attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
}
} // this code will be executed every time the interrupt 0 (pin2) gets low.
void rpm_fan(){
half_revolutions++; }
Thermocouple reader:
include <SparkFunMAX31855k.h> // Using the max31855k driver
include <SPI.h> // Included here too due Arduino IDE; Used in above header
// Define SPI Arduino pin numbers (Arduino Pro Mini) const uint8_t CHIP_SELECT_PIN = 10; // Using standard CS line (SS) // SCK & MISO are defined by Arduiino const uint8_t VCC = 14; // Powering board straight from Arduino Pro Mini const uint8_t GND = 15;
// Instantiate an instance of the SparkFunMAX31855k class SparkFunMAX31855k probe(CHIP_SELECT_PIN, VCC, GND);
void setup() { Serial.begin(9600); Serial.println("\nBeginning..."); delay(50); // Let IC stabilize or first readings will be garbage }
void loop() { float temperature = probe.readCJT();
// Read methods return NAN if they don't have a valid value to return. // The following conditionals only print the value if it's not NAN. if (!isnan(temperature)) { Serial.print("CJT is (˚C): "); Serial.println(temperature); }
// Read the temperature in Celsius temperature = probe.readTempC(); if (!isnan(temperature)) { Serial.print("Temp
="); Serial.print(temperature); }
// Read the temperature in Fahrenheit temperature = probe.readTempF(); if (!isnan(temperature)) { Serial.print("\tTemp[F]="); Serial.print(temperature); }
// Read the temperature in Kelvin temperature = probe.readTempK(); if (!isnan(temperature)) { Serial.print("\tTemp[K]="); Serial.print(temperature); }
// Read the temperature in Rankine temperature = probe.readTempR(); if (!isnan(temperature)) { Serial.print("\tTemp[R]="); Serial.println(temperature); }
delay(20000); }