Hi all,
I am working on a project where I am using four sensors connected to an Arduino Mega. The first three sensors are thermocouple amplifiers and they connect to digital inputs on the Mega and are controlled via SPI. The amplifier is the Max31855. I have code written to output the amplifiers values to the Nextion display continuously and this works exactly how I want it to work.
The problem that I am having is getting the display to display the output from the force sensitive resistor. It just uses a simple voltage divider with a 10K resistor and one analog input.
I have written some code for it and Im thinking that it should output onto the screen like the thermocouples but it does not the value just stays at zero.
I am using a number variable for the force sensor and assigning fsrForce to "force.val="
If I look on the serial monitor I can see that the force.val= 2?? so I know that the arduino understands this and is trying to get it onto the display but for some reason it cannot.
Any help would be greatly appreciated I am on a strict schedule sadly however.
Thanks in advance.
Here is my code ignore all the other not so relevant stuff in it. It does have the code to communicate with the LCD for the thermocouples which works fine its the force part that Ive added thats the issue.
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Adafruit_MAX31855.h>
#define Enable 31
#define dirPin 3
#define stepPin 4
#define DischargePB 46
#define DischargePB2 47
#define stepsPerRevolution 1600
#define MAXDO 30
#define MAXCS 27
#define MAXCLK 28
#define MAXCS2 34
#define MAXDO2 35
#define MAXCS3 38
#define MAXDO3 39
int switchstate1 = 0;
int switchstate2 = 0;
int fsrPin = 5; // the FSR and 10K pulldown are connected to a5
int fsrReading; // the analog reading from the FSR resistor divider
int fsrVoltage; // the analog reading converted to voltage
unsigned long fsrResistance; // The voltage converted to resistance
unsigned long fsrConductance;
int fsrForce; //the resistance converted to force
Adafruit_MAX31855 thermocouple(MAXCLK, MAXCS, MAXDO);// MAXCLK = Turns on the thermocouple amplifier chip MAXCS = Clock cycle all three thermocouple amplifiers, MAXDO = Output voltage from amplifier
Adafruit_MAX31855 thermocouple2(MAXCLK, MAXCS2, MAXDO2);
Adafruit_MAX31855 thermocouple3(MAXCLK, MAXCS3, MAXDO3);
void setup() {
pinMode(Enable, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(DischargePB, INPUT);
pinMode(DischargePB2, INPUT);
while (!Serial); // wait for Serial on Leonardo/Zero, etc
Serial.begin(9600);
}
void loop() {
switchstate1 = digitalRead(DischargePB);
switchstate2 = digitalRead(DischargePB2);
double temperature = thermocouple.readCelsius();
if (isnan(temperature)) {
Serial.println("Something wrong with thermocouple!"); // Error check checks to see if a wire has disconnected from the terminal block
} else {
String command = "temperature.txt=""+String(temperature,1 )+"""; // This is the code used to store the temperature and send to the display
Serial.print(command);
endNextionCommand();
}
double temperature2 = thermocouple2.readCelsius();
if (isnan(temperature2)) {
Serial.println("Something wrong with thermocouple!"); // Error check checks to see if a wire has disconnected from the terminal block
} else {
String command = "temperature2.txt=""+String(temperature2,1 )+"""; // This is the code used to store the temperature and send to the display
Serial.print(command);
endNextionCommand();
}
double temperature3 = thermocouple3.readCelsius();
if (isnan(temperature3)) {
Serial.println("Something wrong with thermocouple!"); // Error check checks to see if a wire has disconnected from the terminal block
} else {
String command = "temperature3.txt=""+String(temperature3,1 )+"""; // This is the code used to store the temperature and send to the display
Serial.print(command);
endNextionCommand();
}
fsrReading = analogRead(fsrPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// analog voltage reading ranges from about 0 to 1023 which maps to 0V to 5V (= 5000mV)
fsrVoltage = map(fsrReading, 0, 1023, 0, 5000);
Serial.print("Voltage reading in mV = ");
Serial.println(fsrVoltage);
fsrResistance = 5000 - fsrVoltage; // fsrVoltage is in millivolts so 5V = 5000mV
fsrResistance *= 10000; // 10K resistor
fsrResistance /= fsrVoltage;
Serial.print("FSR resistance in ohms = ");
Serial.println(fsrResistance);
fsrConductance = 1000000; // we measure in micromhos
fsrConductance /= fsrResistance;
Serial.print("Conductance in microMhos: ");
Serial.println(fsrConductance);
// Use the two FSR guide graphs to approximate the force
if (fsrConductance <= 1000) {
fsrForce = fsrConductance / 80;
Serial.print("force.val=");
Serial.print(fsrForce);
endNextionCommand();
}
else {
fsrForce = fsrConductance - 1000;
fsrForce /= 30;
Serial.print("force.val=");
Serial.print(fsrForce);
endNextionCommand();
}
digitalWrite (Enable, HIGH);
if (switchstate1 == LOW and switchstate2 == LOW)
{
subroutine1();
}
}
void endNextionCommand() // Subroutine used to send the data to the Nextion screen
{ delay(250);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
void subroutine1(){
digitalWrite(Enable, LOW);
digitalWrite(dirPin, HIGH);
for (int i = 0; i < 0.58 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
digitalWrite(dirPin, LOW);
for (int i = 0; i < 0.58 * stepsPerRevolution; i++) {
digitalWrite(stepPin, HIGH);
delayMicroseconds(500);
digitalWrite(stepPin, LOW);
delayMicroseconds(500);
}
delay(1000);
} //dirPin HIGH = CW dirPin LOW = CCW