M5stack led output not working.



  • Hello guys,
    i am trying to create a menu using Arduion and M5stack, i wanted to test the buttons if they work, so i conected a led pin to 21 and G output, when i press start the led should go on and if i press stop the led should turn off, but it is not working how i want, the led is always on no matter what i press, but if i switch the pin output the led starts to blink when i press start or stop ,
    what i am trying to create is switching the led on or off when i press start or stop
    here is my code:

    bool LED_ON = false;
    int clockwise = 0;
    int counterclockwise = 0;
    int rest = 0;

    const int ledPin = 21;

    // Standard Arduino setup function. ez.begin() takes care of startup and initialization
    void setup() {
    #include <themes/default.h> // per https://github.com/M5ez/M5ez#themes
    #include <themes/dark.h> // makes both themes available
    pinMode(ledPin, OUTPUT);
    ez.begin(); // initializes everything
    }

    void loop() {

    ez.addEvent(LEDMotor);

    Serial.println("entering loop()"); // Shows what function we're in on the Serial Monitor
    ezMenu topmenu("Top Menu"); // creates the menu but does nothing else
    topmenu.txtSmall(); // makes menu text smaller, neater
    topmenu.buttons("up # select # down"); // 'select' sets pickName and pickCaption, all set pickButton
    //topmenu.addItem("dark | Dark Theme", dark); // ignore in while loop, calls dark() automatically
    //topmenu.addItem("light | Light Theme", light); // ignore in while loop, calls light() automatically
    topmenu.addItem("P1"); // does nothing; handled manually in while loop
    topmenu.addItem("P2"); // does nothing; handled manually in while loop
    topmenu.addItem("P3"); // does nothing; handled manually in while loop
    //topmenu.addItem("Black"); // does nothing; handled manually in while loop
    topmenu.addItem("Menu"); // does nothing; handled manually in while loop
    while(topmenu.runOnce()) { // runOnce displays the menu and returns when a button is pressed
    // you can watch the results in the Serial Monitor
    Serial.printf("topmenu.pick = %d\n", topmenu.pick());
    Serial.printf("topmenu.pickButton = %s\n", topmenu.pickButton().c_str());
    Serial.printf("topmenu.pickName = %s\n", topmenu.pickName().c_str());
    Serial.printf("topmenu.pickCaption = %s\n\n", topmenu.pickCaption().c_str());

    String result  = topmenu.pickName();              // find out what menu was selected when 'select' button was pressed
    if(     result == "P1")   newSubMenu(5,10,5,result);
    else if(result == "P2")   newSubMenu(5,17,3,result);    
    else if(result == "P3")   newSubMenu(10,10,0,result);    
    

    }
    }

    /****/
    void newSubMenu(int clockwise, int counterclockwise, int pause, String menuname)
    {
    Serial.println(menuname); // Shows what function we're in on the Serial Monitor
    ezMenu submenu(menuname); // creates the menu but does nothing else
    submenu.txtSmall(); // makes menu text smaller, neater
    submenu.buttons("up # back # select ## down #"); // standard buttons for a submenu; long press on button A pops up one level
    submenu.addItem("Start"); // not handled at all, so nothing happens when this menu is selected except a redraw
    submenu.addItem("Stop"); // not handled at all, so nothing happens when this menu is selected except a redraw

    while(submenu.runOnce()) { // runOnce displays the menu and returns when a button is pressed
    // you can watch the results in the Serial Monitor
    Serial.printf("submenu.pick = %d\n", submenu.pick());
    Serial.printf("submenu.pickButton = %s\n", submenu.pickButton().c_str());
    Serial.printf("submenu.pickName = %s\n", submenu.pickName().c_str());
    Serial.printf("submenu.pickCaption = %s\n\n", submenu.pickCaption().c_str());

    if(submenu.pickName() == "Start") Motor(clockwise, counterclockwise, true); 
    if(submenu.pickName() == "Stop") Motor(clockwise, counterclockwise, false); 
    

    }
    }

    /* */
    void Motor(int clockwise, int counterclockwise, bool spinmotor)
    {
    LED_ON = spinmotor;
    }

    uint16_t LEDMotor()
    {
    if(LED_ON){
    digitalWrite(ledPin, HIGH);
    Serial.println("LED on");
    }
    else{
    digitalWrite(ledPin, LOW);
    Serial.printlb("LED off");
    }

    return 1000;
    

    }

    /********************************************************************************************************/

    /****/
    void subMenu() { Serial.println("Menu"); // Shows what function we're in on the Serial Monitor
    ezMenu submenu("Menu"); // creates the menu but does nothing else
    submenu.txtSmall(); // makes menu text smaller, neater
    submenu.buttons("up # back # select ## down #"); // standard buttons for a submenu; long press on button A pops up one level
    submenu.addItem("foo"); // not handled at all, so nothing happens when this menu is selected except a redraw
    submenu.addItem("bar"); // not handled at all, so nothing happens when this menu is selected except a redraw
    submenu.addItem("baz"); // not handled at all, so nothing happens when this menu is selected except a redraw
    submenu.addItem("sub | subSubMenu"); // returns the name "sub" but displays the caption "subSubMenu"
    while(submenu.runOnce()) { // runOnce displays the menu and returns when a button is pressed
    // you can watch the results in the Serial Monitor
    Serial.printf("submenu.pick = %d\n", submenu.pick());
    Serial.printf("submenu.pickButton = %s\n", submenu.pickButton().c_str());
    Serial.printf("submenu.pickName = %s\n", submenu.pickName().c_str());
    Serial.printf("submenu.pickCaption = %s\n\n", submenu.pickCaption().c_str());

    if(submenu.pickName() == "sub") subSubMenu();     // submenu.pickName() == "sub", or submenu.pickCaption() == "subSubMenu", or submenu.pick == 4
    

    }
    }

    void subSubMenu() {}



  • Hello @lasslop

    GPIO21 (pin 21) is not a lucky choice. By default GPIO21 is used as SDA (I2C). Have you tried with a different GPIO?

    Thanks
    Felix