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.