Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

Thread: Blink without delay

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    12

    Blink without delay

    Good Afternoon

    looked at the blink without delay. I feel I understand the function but it isn't working for me I don't understand why

    Code:
    #include <Arduino.h>
    #include <potentiometer.h>
    #include <OLED.h>
    #include <rtc.h>
    //#include <LED.h>
    #include <wire.h>
    //*****************************************Declare*****************************//
    // Function prototyping
    const int timer12 = 13; // setting timer to ditigal pin 13 for LED example
    void setup();
    int replaystate = LOW;            // a state for the relay
    unsigned long previousMillis = 0; // stores last updated vaule of relay
    const long interval12 = 2000;
    void timer();
    //*****************************************Declare*****************************//
    //******************************************Main******************************//
    
    int main()
    {
    
      Potentiometer PotentiometerO;
      OLED OLEDO;
      OLEDO.intdisplay();
      displaytime();
      //ledblink();
      timer(); // Will this allow while loop to run or will it not before the void loop will not end?
    
      while (true)
      {
    
        PotentiometerO.getpot();
        OLEDO.OLEDdraw(PotentiometerO.getpot());
      }
      return (0);
    } // end int main
    //******************************************Main******************************//
    //******************************************Setup*****************************//
    void setup()
    {
      // set the digital relay pin an output
      pinMode(timer12, OUTPUT); // setting pin 3 to an output pin
    } //end setup
    //******************************************Setup*****************************//
    //*****************************************Functions**************************//
    void timer()
    {
      //check to see if it is time to turn on the relay
      unsigned long currentMillis = millis();
      if ((currentMillis - previousMillis) >= interval12)
      {
        // save the last time you turned on the relay
        previousMillis = currentMillis;
    
        // if the relay is off turn it on and vice-versa:
        if (replaystate == LOW)
        {
    
          replaystate = HIGH;
        }
        else
        {
    
          replaystate = LOW;
        }
        digitalWrite(timer12, replaystate); // setting the relay state to the realy pinout
      }
    } // end timer
      //*****************************************Functions**************************//

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,398
    … isn't working for me …
    details needed.

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,069
    Your main() function will override and replace the one automatically supplied by Teensyduino. It sets up the hardware - including the timer for millis() amongst other things.
    There is no loop function because your main() function didn't call one. And setup() won't be called because your main function didn't call it either.
    Best thing to do is get rid of your main function altogether and rewrite your code using the setup() and loop() "paradigm".

    Pete

  4. #4
    Junior Member
    Join Date
    Mar 2019
    Posts
    12
    Quote Originally Posted by el_supremo View Post
    Your main() function will override and replace the one automatically supplied by Teensyduino. It sets up the hardware - including the timer for millis() amongst other things.
    There is no loop function because your main() function didn't call one. And setup() won't be called because your main function didn't call it either.
    Best thing to do is get rid of your main function altogether and rewrite your code using the setup() and loop() "paradigm".

    Pete
    So yes i'm really new to coding and c++ in general

    before I make all of these changes, I though that int main with return (0) is basically just a void loop function.

    But after reading your post over and over again I feel that this void loop () for your main program has things in the background that it calls to set everything up.

    Thank you so much for asking, if this is right I will work on getting my code fixed to have void main over int main.

  5. #5
    Junior Member
    Join Date
    Mar 2019
    Posts
    12
    The code started working when I used the void loop.


    Thank you everyone, I hope this will fix my time issue I was having to


    Joe

  6. #6
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    8,398
    Good eye/focus Pete - I didn't even look at the code open …

    @Dellyjoe - in the IDE there are Examples for code that show the general use of setup() and loop() - that is the standard Arduino way - where Arduino owns main() to make the startup uniform for the device and user.

    That special organization in the sketch .INO makes it different from normal c/C++ code, after that or beyond that it is all done with a standard C++ build system.

  7. #7
    Junior Member
    Join Date
    Mar 2019
    Posts
    12
    Quote Originally Posted by defragster View Post
    Good eye/focus Pete - I didn't even look at the code open …

    @Dellyjoe - in the IDE there are Examples for code that show the general use of setup() and loop() - that is the standard Arduino way - where Arduino owns main() to make the startup uniform for the device and user.

    That special organization in the sketch .INO makes it different from normal c/C++ code, after that or beyond that it is all done with a standard C++ build system.
    ok thank you !!

    Now I will learn how to only update the display after the pot value has changed !! This stuff is really exciting !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •