Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    Mobile and Embedded
    3
    11
    1986
    Loading More Posts
    • 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
      jkprog last edited by

      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 Reply Quote 0
      • mrjj
        mrjj Lifetime Qt Champion last edited by

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

        1 Reply Last reply Reply Quote 1
        • J
          jkprog last edited by

          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.

          mrjj 1 Reply Last reply Reply Quote 1
          • mrjj
            mrjj Lifetime Qt Champion @jkprog last edited by mrjj

            @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 Reply Quote 2
            • J
              jkprog last edited by

              Running this program gives the following error:

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

              1 Reply Last reply Reply Quote 0
              • mrjj
                mrjj Lifetime Qt Champion last edited by

                Note
                Write wants char * or Qbytearray

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

                1 Reply Last reply Reply Quote 1
                • J
                  jkprog last edited by

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

                  mrjj 1 Reply Last reply Reply Quote 2
                  • mrjj
                    mrjj Lifetime Qt Champion @jkprog last edited by

                    @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 Reply Quote 1
                    • SGaist
                      SGaist Lifetime Qt Champion last edited by

                      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

                      mrjj J 2 Replies Last reply Reply Quote 3
                      • mrjj
                        mrjj Lifetime Qt Champion @SGaist last edited by mrjj

                        @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 Reply Quote 1
                        • J
                          jkprog @SGaist last edited by

                          @SGaist thank you :)

                          1 Reply Last reply Reply Quote 0
                          • First post
                            Last post