Ok so I have a complicated system I am putting together , it is a DYNO for testing horse Power on small gas engines , all of this is trying to run on a Nano board with SD card and LCD display , I have not had memory issues so far BUT I have timing issues I believe , I am not experienced enough to fix this so far.
- main issue : my RPM's are Off bad , I know this has to do with timing and trying to do to many things in the loop , I have tried everything I can think of.
- Is there a solution to this ? If not see #3
- Is it possible to do the RPM data on one NANO #1 and everything else on the other one #2 ( WIth SD card and LCD ) and send the data to the SD card on #2 , BUT If I can have the data on the SD record data from both boards in a CSV file with 2 columns ?? I need to import this data into a chart program.
- I am using A. NANO , B. LCD 1602 I2C , C. SD card board , D. HX711 load cell board , E. Hall Sensor pin 5
This is the RPM code it seems to work just fine by itself
// digital pin 2 is the hall pin
int hall_pin = 5;
// set number of hall trips for RPM reading (higher improves accuracy)
float hall_thresh = 5.0;
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
// make the hall pin an input:
pinMode(hall_pin, INPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// preallocate values for tach
float hall_count = 1.0;
float start = micros();
bool on_state = false;
// counting number of times the hall sensor is tripped
// but without double counting during the same trip
while(true){
if (digitalRead(hall_pin)==0){
if (on_state==false){
on_state = true;
hall_count+=1.0;
}
} else{
on_state = false;
}
if (hall_count>=hall_thresh){
break;
}
}
// print information about Time and RPM
float end_time = micros();
float time_passed = ((end_time-start)/1000000.0);
// Serial.print("Time Passed: ");
Serial.print(time_passed);
Serial.println("s");
float rpm_val = (hall_count/time_passed)*60.0;
Serial.print(rpm_val);
Serial.println(" RPM");
delay(1); // delay in between reads for stability
}
This is the main code ( it has the RPM code in it ) the RPM's are all messed up , they are all over the place timing wise) , is there a way to sort this out ?? )
/*
SD card read/write
This example shows how to read and write data to and from an SD card file
The circuit:
SD card attached to SPI bus as follows:
** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4 (for MKRZero SD: SDCARD_SS_PIN)
*/
// digital pin 5 is the hall pin,
//BRN= +V, Blu=gnd, Blk= load
int hall_pin = 5;
// set number of hall trips for RPM reading (higher improves accuracy)
float hall_thresh = 6; // 10 ?, 5
#include "HX711.h"
#include <Wire.h> // Library for I2C communication
#include <LiquidCrystal_I2C.h> // Library for LCD
// Wiring: SDA pin is connected to A4 and SCL pin to A5.
// Connect to LCD via I2C, default address 0x27 (A0-A2 not jumpered)
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Change to (0x27,20,4) for 20x4 LCD.
#define calibration_factor -9300.0 //This value is obtained using the SparkFun_HX711_Calibration sketch
#define LOADCELL_DOUT_PIN 3
#define LOADCELL_SCK_PIN 2
HX711 scale;
#include <SPI.h>
#include <SD.h>
// Base name must be six characters or less for short file names.
#define FILE_BASE_NAME "Data"
File file;
const uint8_t BASE_NAME_SIZE = sizeof(FILE_BASE_NAME) - 1;
char fileName[] = FILE_BASE_NAME "00.csv";
const int chipSelect = 4;
void setup() {
lcd.begin();
lcd.backlight();
Serial.begin(115200);
if (!SD.begin(chipSelect)) {
Serial.println(F("begin failed"));
return;
}
while (SD.exists(fileName)) {
if (fileName[BASE_NAME_SIZE + 1] != '9') {
fileName[BASE_NAME_SIZE + 1]++;
} else if (fileName[BASE_NAME_SIZE] != '9') {
fileName[BASE_NAME_SIZE + 1] = '0';
fileName[BASE_NAME_SIZE]++;
} else {
Serial.println(F("Can't create file name"));
return;
}
}
// make the hall pin an input:
pinMode(hall_pin, INPUT);
//Serial.println("HX711 scale demo");
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibration_factor); //This value is obtained by using the SparkFun_HX711_Calibration sketch
scale.tare(); //Assuming there is no weight on the scale at start up, reset the scale to 0
//Serial.println("Readings:");
}
void loop() {
float hall_count = 1.0;
float start = micros();
bool on_state = false;
// counting number of times the hall sensor is tripped
// but without double counting during the same trip
while (true) {
if (digitalRead(hall_pin) == 0) {
if (on_state == false) {
on_state = true;
hall_count += 1.0;
}
} else {
on_state = false;
}
if (hall_count >= hall_thresh) {
break;
}
}
// print information about Time and RPM
float end_time = micros();
float time_passed = ((end_time - start) / 1000000.0);
// Serial.print("Time Passed: ");
// Serial.print(time_passed);
// Serial.println("s");
float rpm_val = (hall_count / time_passed) * 60.0;
Serial.print(rpm_val - 60);
Serial.println(" RPM");
// delay(50);
Serial.println(" ' ");
Serial.print(scale.get_units(2), 1); //scale.get_units() returns a float
Serial.print(" OZ "); //You can change this to kg but you'll need to refactor the calibration_factor
Serial.println();
lcd.setCursor(0, 1);
lcd.print("BuzzDyno");
lcd.setCursor(0, 0);
lcd.print("RPM = "); lcd.print(rpm_val - 60);
lcd.setCursor(0, 1);
lcd.print("Load = "); lcd.print(scale.get_units(2), 1);
file = SD.open(fileName, FILE_WRITE);
if (!file) {
Serial.println(F("open failed"));
return;
}
file.print(rpm_val - 60);
file.print(scale.get_units(2), 1);
file.close();
delay(10);
}
IF I am missing anything let me know and THANKS !