Results 1 to 9 of 9

Thread: Any One Code For Arduino? Need Help

  1. #1
    Senior Member
    Join Date
    Aug 2013
    Location
    Enfield, London
    Posts
    277
    Thanks
    82
    Thanked
    17 times in 15 posts
    • TheDutyPaid's system
      • Motherboard:
      • Asus Gryphon Z87
      • CPU:
      • i7 4770K
      • Memory:
      • 16GB
      • Storage:
      • 2TB and 500GB HDD
      • Graphics card(s):
      • Asus 560ti
      • PSU:
      • Corsair 750W
      • Case:
      • 1953 Bush AC-34 Radio now case
      • Operating System:
      • Win 7 64bit
      • Monitor(s):
      • 20" Samsung & 27" ASUS
      • Internet:
      • BT Infinity, Fast :-)

    Any One Code For Arduino? Need Help

    I am putting together a BTTF style speedometer, I have all the parts adafruit ultimate gps, adafruit 0.56 4-digit 7-segment display w/i2c backpack, and an Arduino Nano.
    That all works fine with a modified code from a GPS based clock.

    The problem is, I want need to add a switch which will control the brightness form 0 to 15 and will just cycle through the levels.

    I have asked this question on the Arduino forums, but they was more concerned that I did not rename a pin from the 'buttonpin' to a fancy name switch. lol

    Been on this for about 3 days and it is giving me sleepless nights for some reason.

    Code is below, if you can help I will give you credit on the file


    Code:
    #include <Bounce2.h>
    #include <SoftwareSerial.h>
    #include <Wire.h>
    #include <Adafruit_LEDBackpack.h>
    #include "Adafruit_GFX.h"
    #include <NMEAGPS.h>
    
    
    // I2C address of the display.  Stick with the default address of 0x70
    // unless you've changed the address jumpers on the back of the display.
    #define DISPLAY_ADDRESS   0x70
    
    Adafruit_7segment clockDisplay;
    SoftwareSerial gpsSerial(8, 7);
    NMEAGPS gps;
    
    const byte BlueTheWonderSwitch = 4;
    int Brightness = 1;      // starting brightness
    Bounce brightnessNext(4, BlueTheWonderSwitch);
    
    
    void setup()
    {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(9600);
      Serial.begin(115200);
      Serial.println( F("Clock starting!") ); // F macro saves RAM!
    
      // make the pushbutton's pin an input:
      pinMode(BlueTheWonderSwitch, INPUT);
      pinMode( 4, INPUT_PULLUP ); // UP_PIN
    
      clockDisplay.begin(DISPLAY_ADDRESS);
      clockDisplay.setBrightness(1);
    
      gpsSerial.begin(9600);
      gps.send_P( &gpsSerial, F("PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") ); // RMC only
      gps.send_P( &gpsSerial, F("PMTK220,1000") ); // 1Hz update
    }
    
    void loop()
    {
      if (digitalRead(4))
      {
        if (brightnessNext.update() && brightnessNext.fell()) {
          Brightness += (1);
          if (Brightness > 15) {
            Brightness = (0);
          }
        }
    
        // read the input pin:
        int buttonState = digitalRead(BlueTheWonderSwitch);
        // print out the state of the button:
        Serial.println(buttonState);
        delay(500);        // delay in between reads for stability
    
        // Process GPS chars
        while (gps.available( gpsSerial ))
        {
    
          //  A new fix has been assembled from processed chars
          gps_fix fix = gps.read();
    
          //  Display fix status (the status may not be valid yet)
          Serial.print( F("HELLO: ") );
          if (fix.valid.status)
          Serial.print(fix.status); // 0, 1 or 2 means no fix.  3 is a good fix
          Serial.println();
    
          //  Display fix speed (the speed may not be valid yet)
          Serial.print( F("MPH: ") );
          if (fix.valid.speed) {
    
            // Here is a way to get the whole number MPH
            int speed_mph = (fix.spd.whole * 115) / 100;
    
            // You could get the floating point (with decimals) MPH like this:
            //float speed_mph = fix.speed_mph(88);
            //speed_mph = random( 0, 100 ); // uncomment for testing
    
            clockDisplay.println( speed_mph );
          } else
            clockDisplay.clear();
          clockDisplay.writeDisplay();
          Serial.println();
        }
      }
    }
    Not sure where was best to put this question.

  2. #2
    Senior Member
    Join Date
    Apr 2004
    Location
    The Third Foundation
    Posts
    919
    Thanks
    2
    Thanked
    99 times in 91 posts

    Re: Any One Code For Arduino? Need Help

    I'm not familiar with the APIs involved, but that fell function call jumps out at me. If it's testing for the brightness level rising/falling then it could fail as you're doing both in the loop depending on the cycle.

    What behaviour does the code give you? No change at all, only one cycle or something else?

  3. #3
    root Member DanceswithUnix's Avatar
    Join Date
    Jan 2006
    Location
    In the middle of a core dump
    Posts
    13,009
    Thanks
    781
    Thanked
    1,568 times in 1,325 posts
    • DanceswithUnix's system
      • Motherboard:
      • Asus X470-PRO
      • CPU:
      • 5900X
      • Memory:
      • 32GB 3200MHz ECC
      • Storage:
      • 2TB Linux, 2TB Games (Win 10)
      • Graphics card(s):
      • Asus Strix RX Vega 56
      • PSU:
      • 650W Corsair TX
      • Case:
      • Antec 300
      • Operating System:
      • Fedora 39 + Win 10 Pro 64 (yuk)
      • Monitor(s):
      • Benq XL2730Z 1440p + Iiyama 27" 1440p
      • Internet:
      • Zen 900Mb/900Mb (CityFibre FttP)

    Re: Any One Code For Arduino? Need Help

    Never coded on an Arduino, but just from a quick look:

    Just after your bit where you increment and check Brightness:

    Brightness += (1);
    if (Brightness > 15) {
    Brightness = (0);
    }

    Shouldn't you have something like:

    clockDisplay.setBrightness( Brightness );

    to actually deploy the value?

  4. Received thanks from:

    TheDutyPaid (24-11-2016)

  5. #4
    root Member DanceswithUnix's Avatar
    Join Date
    Jan 2006
    Location
    In the middle of a core dump
    Posts
    13,009
    Thanks
    781
    Thanked
    1,568 times in 1,325 posts
    • DanceswithUnix's system
      • Motherboard:
      • Asus X470-PRO
      • CPU:
      • 5900X
      • Memory:
      • 32GB 3200MHz ECC
      • Storage:
      • 2TB Linux, 2TB Games (Win 10)
      • Graphics card(s):
      • Asus Strix RX Vega 56
      • PSU:
      • 650W Corsair TX
      • Case:
      • Antec 300
      • Operating System:
      • Fedora 39 + Win 10 Pro 64 (yuk)
      • Monitor(s):
      • Benq XL2730Z 1440p + Iiyama 27" 1440p
      • Internet:
      • Zen 900Mb/900Mb (CityFibre FttP)

    Re: Any One Code For Arduino? Need Help

    Quote Originally Posted by EndlessWaves View Post
    I'm not familiar with the APIs involved, but that fell function call jumps out at me. If it's testing for the brightness level rising/falling then it could fail as you're doing both in the loop depending on the cycle.
    That looks to be detecting the de-bounced falling edge of the button press, so button release.

    The setup seems to be the wrong way around, but thankfully the constant for the switch pin is 4 and the debounce time is 4ms so the two luck out as interchangable

  6. #5
    Senior Member
    Join Date
    Aug 2013
    Location
    Enfield, London
    Posts
    277
    Thanks
    82
    Thanked
    17 times in 15 posts
    • TheDutyPaid's system
      • Motherboard:
      • Asus Gryphon Z87
      • CPU:
      • i7 4770K
      • Memory:
      • 16GB
      • Storage:
      • 2TB and 500GB HDD
      • Graphics card(s):
      • Asus 560ti
      • PSU:
      • Corsair 750W
      • Case:
      • 1953 Bush AC-34 Radio now case
      • Operating System:
      • Win 7 64bit
      • Monitor(s):
      • 20" Samsung & 27" ASUS
      • Internet:
      • BT Infinity, Fast :-)

    Re: Any One Code For Arduino? Need Help

    Quote Originally Posted by EndlessWaves View Post
    What behaviour does the code give you? No change at all, only one cycle or something else?
    No change at all, I know the switch is working. But I think you are on to something.

  7. #6
    Senior Member
    Join Date
    Aug 2013
    Location
    Enfield, London
    Posts
    277
    Thanks
    82
    Thanked
    17 times in 15 posts
    • TheDutyPaid's system
      • Motherboard:
      • Asus Gryphon Z87
      • CPU:
      • i7 4770K
      • Memory:
      • 16GB
      • Storage:
      • 2TB and 500GB HDD
      • Graphics card(s):
      • Asus 560ti
      • PSU:
      • Corsair 750W
      • Case:
      • 1953 Bush AC-34 Radio now case
      • Operating System:
      • Win 7 64bit
      • Monitor(s):
      • 20" Samsung & 27" ASUS
      • Internet:
      • BT Infinity, Fast :-)

    Re: Any One Code For Arduino? Need Help

    I cleaned up the code this morning but still with no luck,

    Code:
    #include <Bounce2.h>
    #include <SoftwareSerial.h>
    #include <Wire.h>
    #include <Adafruit_LEDBackpack.h>
    #include "Adafruit_GFX.h"
    #include <NMEAGPS.h>
    
    
    // I2C address of the display.  Stick with the default address of 0x70
    // unless you've changed the address jumpers on the back of the display.
    #define DISPLAY_ADDRESS   0x70
    
    Adafruit_7segment clockDisplay;
    SoftwareSerial gpsSerial(8, 7);
    NMEAGPS gps;
    
    const int BlueTheWonderSwitch = 4;
    Bounce brightnessNext;
    int Brightness = 1;      // starting brightness
    
    void setup()
    {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(115200);
      Serial.println( F("Clock starting!") ); // F macro saves RAM!
    
      // Use the Bounce2 library for the brightness switch|
      brightnessNext.attach( BlueTheWonderSwitch, INPUT_PULLUP );
      brightnessNext.interval( 25 ); // how quickly to sample
    
      clockDisplay.begin(DISPLAY_ADDRESS);
      clockDisplay.setBrightness(1);
    
      gpsSerial.begin(9600);
      gps.send_P( &gpsSerial, F("PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") ); // RMC only
      gps.send_P( &gpsSerial, F("PMTK220,1000") ); // 1Hz update
    }
    
    void loop()
    {
      // Process button presses
      if (brightnessNext.update() && brightnessNext.fell()) {
        Brightness += (1);
        if (Brightness > 15) {
          Brightness = (0);
        }
      }
    
      // Process GPS chars
      while (gps.available( gpsSerial ))
      {
    
        //  A new fix has been assembled from processed chars
        gps_fix fix = gps.read();
    
        //  Display fix status (the status may not be valid yet)
        Serial.print( F("HELLO: ") );
        if (fix.valid.status)
          Serial.print(fix.status); // 0, 1 or 2 means no fix.  3 is a good fix
        Serial.println();
    
        //  Display fix speed (the speed may not be valid yet)
        Serial.print( F("MPH: ") );
        if (fix.valid.speed) {
    
          // Here is a way to get the whole number MPH
          int speed_mph = (fix.spd.whole * 115) / 100;
    
          // You could get the floating point (with decimals) MPH like this:
          //float speed_mph = fix.speed_mph();
          //speed_mph = random( 0, 100 ); // uncomment for testing
    
          clockDisplay.println( speed_mph );
        } else
          clockDisplay.clear();
        clockDisplay.writeDisplay();
        Serial.println();
      }
    
    }

  8. #7
    Not a good person scaryjim's Avatar
    Join Date
    Jan 2009
    Location
    Gateshead
    Posts
    15,196
    Thanks
    1,232
    Thanked
    2,290 times in 1,873 posts
    • scaryjim's system
      • Motherboard:
      • Dell Inspiron
      • CPU:
      • Core i5 8250U
      • Memory:
      • 2x 4GB DDR4 2666
      • Storage:
      • 128GB M.2 SSD + 1TB HDD
      • Graphics card(s):
      • Radeon R5 230
      • PSU:
      • Battery/Dell brick
      • Case:
      • Dell Inspiron 5570
      • Operating System:
      • Windows 10
      • Monitor(s):
      • 15" 1080p laptop panel

    Re: Any One Code For Arduino? Need Help

    Quote Originally Posted by DanceswithUnix View Post
    ... Shouldn't you have something like:

    clockDisplay.setBrightness( Brightness );

    to actually deploy the value? ...
    If this was stack overflow I'd be +1ing this. Definitely the first thing to look at.

    EDIT: I'd also consider replacing
    Code:
    clockDisplay.setBrightness(1);
    in your setup() function with
    Code:
    clockDisplay.setBrightness(Brightness);
    since your comments say you set the starting brightness, but then you don't use it!

    EDIT 2: (since we cross posted!):

    try changing the first block on your loop() function to this (based on DwU's suggestion):
    Code:
      // Process button presses
      if (brightnessNext.update() && brightnessNext.fell()) {
        Brightness += (1);
        if (Brightness > 15) {
          Brightness = (0);
        }
    
        clockDisplay.setBrightness(Brightness);
      }

  9. Received thanks from:

    TheDutyPaid (24-11-2016)

  10. #8
    Senior Member
    Join Date
    Aug 2013
    Location
    Enfield, London
    Posts
    277
    Thanks
    82
    Thanked
    17 times in 15 posts
    • TheDutyPaid's system
      • Motherboard:
      • Asus Gryphon Z87
      • CPU:
      • i7 4770K
      • Memory:
      • 16GB
      • Storage:
      • 2TB and 500GB HDD
      • Graphics card(s):
      • Asus 560ti
      • PSU:
      • Corsair 750W
      • Case:
      • 1953 Bush AC-34 Radio now case
      • Operating System:
      • Win 7 64bit
      • Monitor(s):
      • 20" Samsung & 27" ASUS
      • Internet:
      • BT Infinity, Fast :-)

    Re: Any One Code For Arduino? Need Help

    IT WORKS! Thank you so very much, I can sleep tonight.

    Full finished code if you ever need it below.

    Code:
    //Made with help from the Hexus forum. http://hexus.net/
    
    #include <Bounce2.h>
    #include <SoftwareSerial.h>
    #include <Wire.h>
    #include <Adafruit_LEDBackpack.h>
    #include "Adafruit_GFX.h"
    #include <NMEAGPS.h>
    
    
    // I2C address of the display.  Stick with the default address of 0x70
    // unless you've changed the address jumpers on the back of the display.
    #define DISPLAY_ADDRESS   0x70
    
    Adafruit_7segment clockDisplay;
    SoftwareSerial gpsSerial(8, 7);
    NMEAGPS gps;
    
    const int BlueTheWonderSwitch = 4;
    Bounce brightnessNext;
    int Brightness = 7;      // starting brightness
    
    void setup()
    {
      // initialize serial communication at 9600 bits per second:
      Serial.begin(115200);
      Serial.println( F("Clock starting!") ); // F macro saves RAM!
    
      // Use the Bounce2 library for the brightness switch|
      brightnessNext.attach( BlueTheWonderSwitch, INPUT_PULLUP );
      brightnessNext.interval( 25 ); // how quickly to sample
    
      clockDisplay.begin(DISPLAY_ADDRESS);
      clockDisplay.setBrightness( Brightness );  // uses the initial value from above
    
      gpsSerial.begin(9600);
      gps.send_P( &gpsSerial, F("PMTK314,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") ); // RMC only
      gps.send_P( &gpsSerial, F("PMTK220,1000") ); // 1Hz update
    }
    
    void loop()
    {
      // Process button presses
      if (brightnessNext.update()) {
        Brightness += (1);
        if (Brightness > 15) {
          Brightness = (0);
        }
        clockDisplay.setBrightness( Brightness );
      }
    
      // Process GPS chars
      while (gps.available( gpsSerial ))
      {
    
        //  A new fix has been assembled from processed chars
        gps_fix fix = gps.read();
    
        //  Display fix status (the status may not be valid yet)
        Serial.print( F("HELLO: ") );
        if (fix.valid.status)
          Serial.print(fix.status); // 0, 1 or 2 means no fix.  3 is a good fix
        Serial.println();
    
        //  Display fix speed (the speed may not be valid yet)
        Serial.print( F("MPH: ") );
        if (fix.valid.speed) {
    
          // Here is a way to get the whole number MPH
          int speed_mph = (fix.spd.whole * 115) / 100;
    
          // You could get the floating point (with decimals) MPH like this:
          //float speed_mph = fix.speed_mph();
          //speed_mph = random( 0, 100 ); // uncomment for testing
    
          clockDisplay.println( speed_mph );
        } else
          clockDisplay.clear();
        clockDisplay.writeDisplay();
        Serial.println();
      }
    }

  11. #9
    Senior Member
    Join Date
    Aug 2013
    Location
    Enfield, London
    Posts
    277
    Thanks
    82
    Thanked
    17 times in 15 posts
    • TheDutyPaid's system
      • Motherboard:
      • Asus Gryphon Z87
      • CPU:
      • i7 4770K
      • Memory:
      • 16GB
      • Storage:
      • 2TB and 500GB HDD
      • Graphics card(s):
      • Asus 560ti
      • PSU:
      • Corsair 750W
      • Case:
      • 1953 Bush AC-34 Radio now case
      • Operating System:
      • Win 7 64bit
      • Monitor(s):
      • 20" Samsung & 27" ASUS
      • Internet:
      • BT Infinity, Fast :-)

    Re: Any One Code For Arduino? Need Help

    Just a quick update, The unit is all finished now and works a treat. One thing I did use update the the 1Hz to 10Hz update as getting the speed update once a second is a bit slow.
    Can also be used as a GPS based clock with a software update.


Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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