Send low to all the 60 gpio pins on arduino when a button is pressed



  • I am trying to send LOW to all the GPIO pins of the arduino mega 2560 using a QPushButton. I used the button clicked slot and using serial communication, I am am writing 0 to all the pins using serial port (turning off all the pins) . I am using a baud rate of 9600.
    The problem is, when i click the button, only the first 9 pins turn off. The rest of the pins do not change there state from on to off. When I send high/low signal individually to each pin, it works fine for every pin, but it doesn't work when i try to do the same thing for all the pins simultaneously.

    My code is:

    void MainWindow::on_pushButton_clicked()
    {
        serial.write("<on, 2, 1>");
        serial.write("<on, 3, 1>");
        serial.write("<on, 4, 1>");
        serial.write("<on, 5, 1>");
        serial.write("<on, 6, 1>");
        serial.write("<on, 7, 1>");
        serial.write("<on, 8, 1>");
        serial.write("<on, 9, 1>");
        serial.write("<on, 10, 1>");
        serial.write("<on, 11, 1>");
        serial.write("<on, 12, 1>");
        serial.write("<on, 13, 1>");
        serial.write("<on, 14, 1>");
        serial.write("<on, 15, 1>");
        serial.write("<on, 16, 1>");
        serial.write("<on, 17, 1>");
        serial.write("<on, 18, 1>");
        serial.write("<on, 19, 1>");
        serial.write("<on, 20, 1>");
        serial.write("<on, 21, 1>");
        serial.write("<on, 22, 1>");
        serial.write("<on, 23, 1>");
        serial.write("<on, 24, 1>");
        serial.write("<on, 25, 1>");
        serial.write("<on, 26, 1>");
        serial.write("<on, 27, 1>");
        serial.write("<on, 28, 1>");
        serial.write("<on, 29, 1>");
        serial.write("<on, 30, 1>");
        serial.write("<on, 31, 1>");
    
    }
    
    

  • Qt Champions 2016

    Hi
    Maybe its too much for the board in one go ?



  • Can you suggest me a solution or an alternate way to do it? Because I need to have a button to turn on and off all the gpio pins of the arduino.


  • Qt Champions 2016

    @jkprog
    You could try with a QTimer and only turn 1 off pr timer tick
    so there will be a small delay between each command.

    Like

    class MainWindow : public QMainWindow {
      Q_OBJECT
     public:
      explicit MainWindow(QWidget* parent = 0);
      ~MainWindow();
      QTimer* timer; // <<<<<<<<<< the timer
      int ID = 2; // <<<<<<<<<< pin id 
     public slots:
      void MyTimerSlots(); // <<<<<< for timer to call
     private slots:
      void on_pushButton_clicked();
     private:
      Ui::MainWindow* ui;
    };
    
    and in .cpp
    
    MainWindow::MainWindow(QWidget* parent) :
      QMainWindow(parent),
      ui(new Ui::MainWindow) {
      ui->setupUi(this);
    
      ID = 2; // member
      timer = new QTimer(this);
      connect(timer, SIGNAL(timeout()), this, SLOT(MyTimerSlots()) );
    
    }
    
    void MainWindow::MyTimerSlots() {
      QString OUT = QString  ("<on, %1, 1>").arg(ID++); // create the string
      qDebug() << OUT; // for testing
      //serial.write(OUT); // activate this again.
      if (ID > 32)  { timer->stop(); } // make sure it stops
    }
    
    void MainWindow::on_pushButton_clicked() {
      timer->start(500); // 500 ms. try less later
    }
    

    and you get pr 500 ms

    "<on, 2, 1>"
    "<on, 3, 1>"
    "<on, 4, 1>"
    "<on, 5, 1>"
    "<on, 6, 1>"
    "<on, 7, 1>"
    "<on, 8, 1>"
    "<on, 9, 1>"
    "<on, 10, 1>"
    "<on, 11, 1>"
    "<on, 12, 1>"
    "<on, 13, 1>"
    "<on, 14, 1>"
    "<on, 15, 1>"
    ...

    So the exciting part if this works or its something else.

    the test project.
    https://www.dropbox.com/s/8j6lwdnrcahqd7i/serialtimer.zip?dl=0



  • Running this program gives the following error:

    error: no matching function for call to 'QSerialPort::write(QString&)'
    serial.write(OUT);
    ^


  • Qt Champions 2016

    Note
    Write wants char * or Qbytearray

    you can do
    serial.write(OUT.toStdString().c_str()); // ugly ;)



  • The only change i made to the program was : serial.write(OUT.toStdString().c_str(),OUT.size() );
    And its working.. :-)
    Thank you :-)


  • Qt Champions 2016

    @jkprog
    Super, so i guess the board needed a moment to process the input in between.
    We are using 500 ms. You might be able to lower that to 100-200 for a faster on/off.


  • Lifetime Qt Champion

    Hi,

    OUT.toLatin1() will be way cleaner.


  • Qt Champions 2016

    @SGaist

    Thanks , i can never remember the name
    as i want to to be called toByteArray ;)
    Much cleaner than std::string + c_str()



  • @SGaist thank you :)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.