Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Send low to all the 60 gpio pins on arduino when a button is pressed
Forum Update on Monday, May 27th 2025

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

Scheduled Pinned Locked Moved Solved Mobile and Embedded
11 Posts 3 Posters 2.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    jkprog
    wrote on 28 Jun 2017, 18:25 last edited by
    #1

    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>");
    
    }
    
    
    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 28 Jun 2017, 18:34 last edited by
      #2

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

      1 Reply Last reply
      1
      • J Offline
        J Offline
        jkprog
        wrote on 28 Jun 2017, 18:44 last edited by
        #3

        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.

        M 1 Reply Last reply 28 Jun 2017, 18:54
        1
        • J jkprog
          28 Jun 2017, 18:44

          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.

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 28 Jun 2017, 18:54 last edited by mrjj
          #4

          @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

          1 Reply Last reply
          2
          • J Offline
            J Offline
            jkprog
            wrote on 28 Jun 2017, 20:19 last edited by
            #5

            Running this program gives the following error:

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

            1 Reply Last reply
            0
            • M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 28 Jun 2017, 20:43 last edited by
              #6

              Note
              Write wants char * or Qbytearray

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

              1 Reply Last reply
              1
              • J Offline
                J Offline
                jkprog
                wrote on 28 Jun 2017, 21:36 last edited by
                #7

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

                M 1 Reply Last reply 28 Jun 2017, 22:23
                2
                • J jkprog
                  28 Jun 2017, 21:36

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

                  M Offline
                  M Offline
                  mrjj
                  Lifetime Qt Champion
                  wrote on 28 Jun 2017, 22:23 last edited by
                  #8

                  @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.

                  1 Reply Last reply
                  1
                  • S Offline
                    S Offline
                    SGaist
                    Lifetime Qt Champion
                    wrote on 29 Jun 2017, 07:05 last edited by
                    #9

                    Hi,

                    OUT.toLatin1() will be way cleaner.

                    Interested in AI ? www.idiap.ch
                    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                    M J 2 Replies Last reply 29 Jun 2017, 07:44
                    3
                    • S SGaist
                      29 Jun 2017, 07:05

                      Hi,

                      OUT.toLatin1() will be way cleaner.

                      M Offline
                      M Offline
                      mrjj
                      Lifetime Qt Champion
                      wrote on 29 Jun 2017, 07:44 last edited by mrjj
                      #10

                      @SGaist

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

                      1 Reply Last reply
                      1
                      • S SGaist
                        29 Jun 2017, 07:05

                        Hi,

                        OUT.toLatin1() will be way cleaner.

                        J Offline
                        J Offline
                        jkprog
                        wrote on 30 Jun 2017, 19:26 last edited by
                        #11

                        @SGaist thank you :)

                        1 Reply Last reply
                        0

                        10/11

                        29 Jun 2017, 07:44

                        • Login

                        • Login or register to search.
                        10 out of 11
                        • First post
                          10/11
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • Users
                        • Groups
                        • Search
                        • Get Qt Extensions
                        • Unsolved