working on a torque transducer and I would like to display the maximum torque and hold it for 10 seconds. but I also need to display the current torque at the same time. I'm not even sure where to start...
I don't know where you should start, either. What have you tried?
Maximum torque is found storing a torque value that is greater than the previous stored torque number.
Since you a little vague about where you want to display either torque figure, I can't even guess what to suggest.
Paul
yes sorry, I'm really not sure how to store values in the first place. I'm displaying the values on an LCD screen... I have values for the current torque being transmitted, but I don't know where to start on saving a maximum value.
If you are displaying values, then you certainly must know where the come from.
Paul
Is it important to you, to display the maximum of the last 10 seconds before now, or just the maximum for every 10 second period, regardless of the current time within that interval?
The fundamental logic:
if (sample > savedMaximum) {
savedMaximum = sample;
}
See?
okay, I actually do have this but the savedMaximum just displays the same as the sample. should I move it out of the loop? and for your previous question, I would like the maximum value before now. so if my savedMaximum increases, it counts 10 seconds before resetting.
Then show us your entire sketch. Please do use code tags for that. We aren't in your room to see what you're doing there.
Seven posts to actually get the real story. Pretty bad.
Not sure about that. It's getting to be par on this green.
That is 4 or 5 just this week.
so just for more info, this is the receiver portion of the code. I'm using ESPNOW to transmit the torque values wirelessly then displaying them on another device (both esp8266)
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Structure example to receive data
// Must match the sender structure
typedef struct test_struct {
float x;
} test_struct;
// Create a struct_message called myData
test_struct myData;
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
//Serial.print("Bytes received: ");
//Serial.println(len);
Serial.print("torque: ");
Serial.println(myData.x);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(57600);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
lcd.init();
//turn on backlight and print message
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0); //row then columm
lcd.print("Torque");
}
void loop() {
myData.x= abs(myData.x);
float Maxt;
lcd.setCursor(7,0);
lcd.print (myData.x);
if (myData.x > Maxt) {
Maxt= myData.x;
}
//}
lcd.setCursor(0,3);
lcd.print(Maxt);
}
Reply #8
#include <ESP8266WiFi.h>
#include <espnow.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
// Structure example to receive data
// Must match the sender structure
typedef struct test_struct {
float x;
} test_struct;
// Create a struct_message called myData
test_struct myData;
// Callback function that will be executed when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&myData, incomingData, sizeof(myData));
//Serial.print("Bytes received: ");
//Serial.println(len);
Serial.print("torque: ");
Serial.println(myData.x);
}
void setup() {
// Initialize Serial Monitor
Serial.begin(57600);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for recv CB to
// get recv packer info
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(OnDataRecv);
lcd.init();
//turn on backlight and print message
lcd.backlight();
lcd.clear();
lcd.setCursor(0,0); //row then columm
lcd.print("Torque");
}
void loop() {
myData.x= abs(myData.x);
float Maxt;
lcd.setCursor(7,0);
lcd.print (myData.x);
if (myData.x > Maxt) {
Maxt= myData.x;
}
//}
lcd.setCursor(0,3);
lcd.print(Maxt);
}
You'll have to update a millis() variable every time there is a new maximum.
But you'll have to figure-out what to do when the 10 seconds is up. Do you want to reset the maximum to the current value? What if there was reading "close" the maximum 9 seconds ago?
Maxt must be declared with the 'static' keyword, or it won't reliably maintain its value between iterations of loop(). Or else, make it a global variable.
For what the OP is asking, the 10 seconds is always up. It's a rolling maximum.
i just added a static float and it now holds its max value. thanks for that info! how do you think I could go about making it reset? i think you got it right with the rolling maximum, but just to be clear, it should reset after 10 seconds if no values go higher than the current maximum.
This new surprise requirement would not be necessary for a rolling maximum. If the values suddenly went to zero for 10 seconds, the rolling maximum would inherently reset to zero after 10 seconds.
If the values slowly decrease, the rolling maximum would track them, not reset to zero. But I think that resetting it would make a very confusing user interface.
this code is mostly for testing the hardware I built, I need to see what the peak torque is, then have it reset so I can run another test without opening up the transducer. the reset button won't be easily accessible. there will always be torque being transmitted, so the value shouldn't go to 0 but it should go to the current torque value