Here is the code I am using. I am sure it can be improved a lot. And I still need to find a way to make the checksum show the ”0”.
It calculates RPM, gear, accelerator position, brake position, and temperature. (for now)
int rpm = 0, gear = 0, brake = 0, acceleration = 0, sensorValue1 = 0, sensorValue2 = 0; unsigned long count = 0, lastmillis = 0; volatile int half_revolutions = 0; unsigned long debounceDelay = 200; // the debounce time; unsigned long lastDebounceTime = 0; // the last time the gear was changed
if (digitalRead(11) == HIGH){ if ((millis() - lastDebounceTime) > debounceDelay) { gear += 1; if (gear>6){ gear=6; } lastDebounceTime = millis(); } } if (digitalRead(12) == HIGH){ if ((millis() - lastDebounceTime) > debounceDelay) { gear -= 1; if (gear<1){ gear=1; } lastDebounceTime = millis(); } }
int reading = analogRead(2); // converting that reading to voltage, for 3.3v arduino use 3.3 float voltage = reading * 5.0; voltage /= 1024.0; float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offsetto degrees ((voltage - 500mV) times 100)
sensorValue1 = analogRead(A0); sensorValue2 = analogRead(A1); acceleration = map(sensorValue1, 0, 1023, 0, 100); //maps the input value of 0-1023 to 0-100 brake = map(sensorValue2, 0, 1023, 0, 100); //maps the input value of 0-1023 to 0-100
if (millis() - lastmillis >= 100){ //Uptade every one hundredth of a second. 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. half_revolutions = 0; // Restart the RPM counter lastmillis = millis(); // Uptade lasmillis attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
Comments
And I still need to find a way to make the checksum show the ”0”.
It calculates RPM, gear, accelerator position, brake position, and temperature. (for now)
int rpm = 0, gear = 0, brake = 0, acceleration = 0, sensorValue1 = 0, sensorValue2 = 0;
unsigned long count = 0, lastmillis = 0;
volatile int half_revolutions = 0;
unsigned long debounceDelay = 200; // the debounce time;
unsigned long lastDebounceTime = 0; // the last time the gear was changed
void setup(){
Serial.begin(9600); // 9600
attachInterrupt(0, rpm_fan, FALLING);
pinMode(A0, INPUT); // acceleration
pinMode(A1, INPUT); // brake
pinMode(11, INPUT); // gears +
pinMode(12, INPUT); // gears -
}
void loop(){
if (digitalRead(11) == HIGH){
if ((millis() - lastDebounceTime) > debounceDelay) {
gear += 1;
if (gear>6){
gear=6;
}
lastDebounceTime = millis();
}
}
if (digitalRead(12) == HIGH){
if ((millis() - lastDebounceTime) > debounceDelay) {
gear -= 1;
if (gear<1){
gear=1;
}
lastDebounceTime = millis();
}
}
int reading = analogRead(2); // converting that reading to voltage, for 3.3v arduino use 3.3
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ; //converting from 10 mv per degree wit 500 mV offsetto degrees ((voltage - 500mV) times 100)
sensorValue1 = analogRead(A0);
sensorValue2 = analogRead(A1);
acceleration = map(sensorValue1, 0, 1023, 0, 100); //maps the input value of 0-1023 to 0-100
brake = map(sensorValue2, 0, 1023, 0, 100); //maps the input value of 0-1023 to 0-100
if (millis() - lastmillis >= 100){ //Uptade every one hundredth of a second.
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.
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(0, rpm_fan, FALLING); //enable interrupt
String buffer = "RC2,," + String(count) + "," + rpm + "," + gear + "," + acceleration + "," + brake + "," + temperatureC + ",,,,,,";
String checksum = String (checkSum(buffer), HEX);
String data = "$" + buffer + "*" + checksum;
Serial.println (data);
if (count == 65535){
count = 0;
}
else
count+=1;
}
}
char checkSum(String chars) {
char check = 0;
for (int c = 0; c < chars.length(); c++) {
check = char(check ^ chars.charAt(c));
}
return check;
}
void rpm_fan(){
half_revolutions++;
}