Go Down

Topic: Problem after compilation - Arduino Leonardo crashes (Read 355 times) previous topic - next topic

dealeros

This is my code:

Code: [Select]
#include <PCF8574.h>
#include <Wire.h>

PCF8574 pcf20(0x20); // I2C Expander

class ButtonSend {

    int buttonPin;
    int buttonState;
    int lastButtonState;
    int reading;
    unsigned long lastDebounceTime;
    unsigned long debounceDelay;


    public:

    ButtonSend(int pin) {
        buttonPin = pin;
        lastButtonState = LOW;
        lastDebounceTime = 0;
        debounceDelay = 50;
        pcf20.read(buttonPin);
        pcf20.write(buttonPin, HIGH);

    }

    void Update() {
        reading = pcf20.read(buttonPin);
        if (reading != lastButtonState) {

            lastDebounceTime = millis();
        }

        if ((millis() - lastDebounceTime) > debounceDelay) {
            if (reading != buttonState) {
            buttonState = reading;

            if (buttonState == LOW) {
                Serial.println("click");
            }
            }
        }

        lastButtonState = reading;
    }

};

void setup() {
    pcf20.begin(); // I2C Expander
}
ButtonSend btn1(2);
void loop() {
    btn1.Update();

}


I'm trying to use I2C expander library - PCF8574 inside my class. I use this library https://github.com/RobTillaart/Arduino/tree/master/libraries/PCF8574 It compiles ok, but after uploading to Arduino  it is not responding - not availaible by port COM. To resotre Arduino I need to upload another project just after restart. I think this can be problem with inheritance, but my knowledge about C++ is poor.

westfw


dealeros

You need a Serial.begin() call in setup()

Thank you for the response. Unfortunently it changes nothing. Result is the same...

sterretje

I doubt this has to do with your code.

As suggested above, you need the Serial.begin().You probably also want to wait till the Serial communication is ready.

Code: [Select]
void setup()
{
  pcf20.begin(); // I2C Expander

  Serial.begin(57600);
  while(!Serial);
  Serial.println("Arduino ready");
  delay(2000);
}

void loop()
{
  btn1.Update();
}


You will also need to make sure that there is space in the buffer for sending; if not, you can bring the Leonardo to a near grinding halt if the connection breaks. Below updated part of the Update method.

Code: [Select]
          if (buttonState == LOW)
          {
            if (Serial.availableForWrite() > strlen("click"))
            {
              Serial.println("click");
            }
          }


Next question is what happens in Windows device manager (or the similar stuff in Linux/Mac) after the upload.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up