hello everyone.
I am trying to do a system which has a main menu with 3 options and inside of each option there will be things that will have to be selected using a potentiometer and a push button. While selecting in the menu, everything is working well. However when I try to select inside of estatico() it sometimes doesn't select or sometimes it selects 2 things at the same time ("currente inicial" and "RPM minimas").
Below you can find the code. appreciate your help on this.
#include "HX711.h"
#define seconds() (millis()/1000)
const int potPin = A0; // Potenciômetro conectado ao A0
// Load Cell with HX711
const int LOADCELL_DOUT_PIN = 3;
const int LOADCELL_SCK_PIN = 4;
HX711 scale;
// Hall Sensor
const int hallSensorPin = 2;
int sensorState = 0;
volatile unsigned int rpmCount = 0;
unsigned long previousMillis = 0;
const unsigned long interval = 1000; // Update RPM every second
// Pushbutton
const int buttonPin = 7; // Pino digital 7
int lastButtonState = LOW;
int buttonState = LOW;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 30;
int menuIndex = 0;
const int numMenuItems = 3;
int currente_inicial = 0;
int rpm_min = 0;
int rpm_adjusted = 0;
float PrevTime = 0;
float Duration = 0;
int rpm = 0;
// Declaração antecipada de funções
void estatico();
void manual();
void countRpm();
void currente();
void rpm_selecter();
void RPM();
void setup() {
Serial.begin(9600);
pinMode(potPin, INPUT);
// Initialize the HX711
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(431.311f); // Calibration factor - adjust as needed
scale.tare(); // Reset the scale to 0
// Initialize the Hall sensor pin
pinMode(hallSensorPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(hallSensorPin), countRpm, RISING);
// Initialize the button pin
pinMode(buttonPin, INPUT_PULLUP);
updateMenu();
}
void loop() {
int potValue = analogRead(potPin);
int newMenuIndex = map(potValue, 1023, 0, 0, numMenuItems); // Mapeamento ajustado
if (newMenuIndex >= numMenuItems) {
newMenuIndex = numMenuItems - 1;
}
if (newMenuIndex != menuIndex) {
menuIndex = newMenuIndex;
updateMenu();
}
// Read the state of the pushbutton
int reading = digitalRead(buttonPin);
// Check for button press with debouncing
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) { // Button pressed
selectMenuItem();
if (menuIndex == 0) {
estatico();
} else if (menuIndex == 1) {
Serial.println("Ainda não feito.");
} else {
manual();
}
}
}
}
lastButtonState = reading;
}
void updateMenu() {
Serial.print("Menu: ");
switch (menuIndex) {
case 0:
Serial.println("1- Estático");
break;
case 1:
Serial.println("2- Transiente");
break;
case 2:
Serial.println("3- Manual");
break;
}
}
void selectMenuItem() {
// Placeholder para a ação quando um item de menu é selecionado
Serial.println("Selecionado!");
delay(1000);
updateMenu();
}
void estatico() {
while (true) {
int cond = 1;
while(cond == 1){
int new_currente_inicial = map(analogRead(potPin), 1023, 0, 0, 30);
if(new_currente_inicial != currente_inicial){
currente_inicial = new_currente_inicial;
currente();
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) { // Button pressed
cond = 0;
}
}
}
lastButtonState = reading;
delay(1000);
}
delay(1000);
cond = 1;
int new_rpm_min = 0;
while(cond == 1){
new_rpm_min = map(analogRead(potPin), 1023, 0, 0, 2000);
if(rpm_min - new_rpm_min > 10 || new_rpm_min - rpm_min > 10) {
rpm_min = new_rpm_min;
rpm_selecter();
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) { // Button pressed
cond = 0;
}
}
}
lastButtonState = reading;
delay(1000);
}
Serial.println("O teste vai ser começar!");
Serial.println("Garante que o acelerador está na posição a testar");
//Falta aqui a parte do potenciometro digital
cond = 1;
while(cond == 1){
int tempo = seconds();
while(seconds() - tempo < 60){
RPM();
// Load Cell Code
// Read the weight
float weight = scale.get_units(10); // Average of 10 readings
// Print the RPM and weight on the Serial Monitor
long tempo_atual =(millis() * 1e-3 -tempo);
Serial.print(tempo_atual);
Serial.print(",");
Serial.print(rpm);
Serial.print(",");
Serial.println(weight);
sensorState = digitalRead(hallSensorPin); // Lê o estado do sensor
}
//Falta parte do potenciometro digital
delay(5000);
RPM();
if (rpm < rpm_adjusted){
//Falta tirar corrente do potenciometro
Serial.println("Teste parado, atingiu rpm minimas");
cond=0;
}
}
return;
}
}
void manual() {
while(true){
int cond = 0;
Serial.println("Atenção, neste teste não há limite!");
Serial.println("Cuidado para não danificar o motor!");
unsigned long tempo = seconds();
while(cond==0){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Calculate RPM (rpmCount is the number of pulses in the last interval)
unsigned int rpm = rpmCount * 60; // Convert to RPM
rpmCount = 0; // Reset count
// Load Cell Code
// Read the weight
float weight = scale.get_units(10); // Average of 10 readings
// Print the RPM and weight on the Serial Monitor
float tempo_atual =(seconds()-tempo);
Serial.print(tempo_atual);
Serial.print(",");
Serial.print(rpm);
Serial.print(",");
Serial.println(weight);
sensorState = digitalRead(hallSensorPin); // Lê o estado do sensor
}
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) { // Button pressed
cond = 1;
}
}
}
lastButtonState = reading;
}
return;
}
}
void countRpm() {
Duration = micros() - PrevTime; // Calculates time difference between revs in microsecond
PrevTime = micros();
}
void currente() {
Serial.print("Corrente inicial = ");
Serial.println(currente_inicial);
}
void RPM()
{
rpm = 60000000 / Duration; // rpm = (1/ time millis)*1000*1000*60;
if (micros() - PrevTime > 2*1000000) // Check if motor stopped - unchanged after 2s
{
rpm = 0;
}
}
void rpm_selecter() {
Serial.print("RPM mínimas = ");
rpm_adjusted = round(rpm_min/50)*50;
Serial.println(rpm_adjusted);
}