The problem with starting and stopping the circuit through the 2 buttons

I have the following problem in my arduino project. When I power the 2 arduino master and slave via usb, the LCD lights up, I press the start button and the 3 parameters appear on the LCD, when I press the stop button on the LCD it displays system stopped, from this moment when I want to restart the project and press the start button again nothing happens, instead if I press the stop button on the lcd again the 3 parameters reappear, practically when the start button is pressed again the circuit should restart and the parameters reappear on the lcd, to nothing happens to me, it restarts if I press the stop button again. Thanks in advance for your help.

Code master:
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>

#define DHTPIN A1
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

#define SS 10
#define LED_RED_PIN 8
#define BUTTON_START_PIN 7

bool isRunning = false;
unsigned long lastButtonPressTime = 0;
unsigned long debounceDelay = 50;

void setup() {
    Serial.begin(9600);
    dht.begin();
    pinMode(SS, OUTPUT);
    pinMode(LED_RED_PIN, OUTPUT);
    pinMode(BUTTON_START_PIN, INPUT_PULLUP);
    digitalWrite(SS, HIGH);
    SPI.begin();
    SPI.setDataMode(SPI_MODE0);
    SPI.setClockDivider(SPI_CLOCK_DIV8);
}

void sendFloat(float value) {
    byte *data = (byte*)&value;
    for (int i = 0; i < sizeof(float); i++) {
        SPI.transfer(data[i]);
        delay(10);  // Mici întârzieri pentru stabilizarea transferului
    }
}

void loop() {
    // Citeste starea butonului de pornire
    bool startButtonPressed = digitalRead(BUTTON_START_PIN) == LOW;

    if (startButtonPressed && (millis() - lastButtonPressTime) > debounceDelay) {
        isRunning = !isRunning; // Toggle the running state
        lastButtonPressTime = millis();
    }

    if (isRunning) {
        float temperature = dht.readTemperature();
        float humidity = dht.readHumidity();
        float current = analogRead(A0) * (5.0 / 1023.0);

        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.print(", Humidity: ");
        Serial.print(humidity);
        Serial.print(", Current: ");
        Serial.println(current);

        digitalWrite(LED_RED_PIN, HIGH);  // Aprinde LED-ul roșu
        digitalWrite(SS, LOW);
        delay(10);

        sendFloat(temperature);
        sendFloat(humidity);
        sendFloat(current);

        digitalWrite(SS, HIGH);
        digitalWrite(LED_RED_PIN, LOW);  // Stinge LED-ul roșu

        delay(1000);
    }
}

Code slave:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SPI.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);

#define SS 10
#define LED_GREEN_PIN 7
#define BUTTON_STOP_PIN 6

bool isRunning = false; // Start with the system stopped
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
    Serial.begin(9600);
    lcd.init();
    lcd.backlight();
    lcd.clear(); // Clear the LCD at startup
    pinMode(SS, INPUT_PULLUP);
    pinMode(SCK, INPUT);
    pinMode(MOSI, INPUT);
    pinMode(MISO, OUTPUT);
    pinMode(LED_GREEN_PIN, OUTPUT);
    pinMode(BUTTON_STOP_PIN, INPUT_PULLUP);
    SPCR = (1 << SPE); // Enable SPI slave
}

float receiveFloat() {
    byte data[sizeof(float)];
    for (int i = 0; i < sizeof(float); i++) {
        while (!(SPSR & (1 << SPIF))); // Așteaptă finalizarea recepției
        data[i] = SPDR; // Citește byte-ul primit
        delay(10); // Mici întârzieri pentru stabilizarea transferului
    }
    return *(float*)data; // Convertește byte-urile în float
}

void loop() {
    int buttonState = digitalRead(BUTTON_STOP_PIN);
    if (buttonState == LOW && (millis() - lastDebounceTime) > debounceDelay) {
        isRunning = !isRunning;
        lastDebounceTime = millis();
    }

    if (isRunning) {
        while (digitalRead(SS)) delay(1); // Așteaptă ca SS să fie LOW

        float temperature = receiveFloat();
        float humidity = receiveFloat();
        float current = receiveFloat();

        Serial.print("Received Temperature: ");
        Serial.print(temperature);
        Serial.print(", Humidity: ");
        Serial.print(humidity);
        Serial.print(", Current: ");
        Serial.println(current);

        digitalWrite(LED_GREEN_PIN, HIGH);  // Aprinde LED-ul verde

        lcd.clear();
        lcd.setCursor(0, 0);
        lcd.print("T: ");
        lcd.print(temperature);
        lcd.print(" H: ");
        lcd.print(humidity);

        lcd.setCursor(0, 1);
        lcd.print("C: ");
        lcd.print(current);

        digitalWrite(LED_GREEN_PIN, LOW);  // Stinge LED-ul verde

        while (!digitalRead(SS)) delay(1); // Așteaptă ca SS să fie HIGH
    } else {
        lcd.clear();  // Curăță LCD-ul
        lcd.setCursor(0, 0);
        lcd.print("System Stopped");

        // Oprește LED-ul verde dacă sistemul este oprit
        digitalWrite(LED_GREEN_PIN, LOW);
    }
}

2 masters and slave sounds not good.
Please provide system and code dokumentation giving an overview.

1 Like

I have one master and one slave.In the electrical diagram I have 2 arduino uno, an arduino master and an arduino slave.la arduino master I connected 2 sensors, namely acs712 and dht11. LA arduino slave I connected the i2c and lcd.the master transmits the data read from the sensors through spi to the arduino slave. The slave transmits the data received from the master via i2c to the lcd and displays it on it.To the project I also have connected 2 buttons for stopping and starting the circuit, the start button is connected to the master, the stop button to the slaves, a red led connected to the master that turns on and off when the master transmits the data to the slaves and a green button that turns on and turning off the slave transmits the data to the lcd through i2c. At the cathodes of the 2 LEDs we have connected a resistor.

You might want to look at this How to get the best out of this forum before you proceed any further.
We only know what you tell us, and without knowing what you have, we don't stand a chance.

Word salad does not cut it in electronics we need to see a schematic. Hand drawn is fine Frtizing style is not.

You have posted lots of words. Repeating and adding more words doesn't improve the post.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.