-1

I'm very new to Arduino and C++ and have been playing with the Flora Wearable GPS. However, nowhere I've looked has given me any help in setting and changing the color of the LEDs. The tutorial on their site is barebones and doesn't explain how to modify code and make the colors change. I've tried mixing what I know, but none of it results in what I want: one color to show when I'm in range of a a point, and another to show when I'm not.

// Flora GPS + LED Pixel Code
//
// This code shows how to listen to the GPS module in an interrupt
// which allows the program to have more 'freedom' - just parse
// when a new NMEA sentence is available! Then access data when
// desired.
//
// Tested and works great with the Adafruit Flora GPS module
//    ------> http://adafruit.com/products/1059
// Pick one up today at the Adafruit electronics shop 
// and help support open source hardware & software! -ada

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include "Adafruit_NeoPixel.h"
Adafruit_GPS GPS(&Serial1);

// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console
// Set to 'true' if you want to debug and listen to the raw GPS sentences
#define GPSECHO false

#define PIN 6

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;

//--------------------------------------------------|
//                    WAYPOINTS                     |
//--------------------------------------------------|
//Please enter the latitude and longitude of your   |
//desired destination:                              |
  #define GEO_LAT                44.995012
  #define GEO_LON               -93.228967
//--------------------------------------------------|

//--------------------------------------------------|
//                    DISTANCE                      |
//--------------------------------------------------|
//Please enter the distance (in meters) from your   |
//destination that you want your LEDs to light up:  |
  #define DESTINATION_DISTANCE   20
//--------------------------------------------------|


// Navigation location
float targetLat = GEO_LAT;
float targetLon = GEO_LON;

// Trip distance
float tripDistance;

boolean isStarted = false;

// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);


uint8_t LED_Breathe_Table[]  = {   80,  87,  95, 103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220,
              228, 236, 242, 247, 251, 254, 255, 255, 254, 251, 247, 242, 236, 228, 220, 211,
              202, 192, 182, 172, 161, 151, 141, 131, 121, 112, 103,  95,  87,  80,  73,  66,
               60,  55,  50,  45,  41,  38,  34,  31,  28,  26,  24,  22,  20,  20,  20,  20,
               20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,
               20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  22,  24,  26,  28,
               31,  34,  38,  41,  45,  50,  55,  60,  66,  73 };


#define BREATHE_TABLE_SIZE (sizeof(LED_Breathe_Table))
#define BREATHE_CYCLE    5000      /*breathe cycle in milliseconds*/
#define BREATHE_UPDATE    (BREATHE_CYCLE / BREATHE_TABLE_SIZE)
uint32_t lastBreatheUpdate = 0;
uint8_t breatheIndex = 0;

void setup()  
{
  // connect at 115200 so we can read the GPS fast enough and echo without dropping chars
  // also spit it out
  Serial.begin(115200);

  // 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800
  GPS.begin(9600);

  // uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  // uncomment this line to turn on only the "minimum recommended" data
  //GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);
  // For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since
  // the parser doesn't care about other sentences at this time

  // Set the update rate
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   // 1 Hz update rate
  // For the parsing code to work nicely and have time to sort thru the data, and
  // print it out we don't suggest using anything higher than 1 Hz

  delay(1000);
  // Ask for firmware version
  Serial1.println(PMTK_Q_RELEASE);

    // Start up the LED strip
  strip.begin();

  // Update the strip, to start they are all 'off'
  strip.show();
}

uint32_t timer = millis();

void loop()                     // run over and over again
{
  // read data from the GPS in the 'main loop'
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
  if (GPSECHO)
      if (c) Serial.print(c);

  // if a sentence is received, we can check the checksum, parse it...
  if (GPS.newNMEAreceived()) {
    // a tricky thing here is if we print the NMEA sentence, or data
    // we end up not listening and catching other sentences! 
    // so be very wary if using OUTPUT_ALLDATA and trytng to print out data
    //Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false

    if (!GPS.parse(GPS.lastNMEA()))   // this also sets the newNMEAreceived() flag to false
      return;  // we can fail to parse a sentence in which case we should just wait for another
  }


    if (GPS.fix) {
      //Serial.print("Location: ");
      //Serial.print(GPS.latitude, 2); Serial.print(GPS.lat);
      //Serial.print(", "); 
      //Serial.print(GPS.longitude, 2); Serial.println(GPS.lon);

      float fLat = decimalDegrees(GPS.latitude, GPS.lat);
      float fLon = decimalDegrees(GPS.longitude, GPS.lon);

      if (!isStarted) {
        isStarted = true;
        tripDistance = (double)calc_dist(fLat, fLon, targetLat, targetLon);
      }

      //Uncomment below if you want your Flora to navigate to a certain destination.  Then modify the headingDirection function.
      /*if ((calc_bearing(fLat, fLon, targetLat, targetLon) - GPS.angle) > 0) {
        headingDirection(calc_bearing(fLat, fLon, targetLat, targetLon)-GPS.angle);
       }
      else {
        headingDirection(calc_bearing(fLat, fLon, targetLat, targetLon)-GPS.angle+360);
      }*/

      headingDistance((double)calc_dist(fLat, fLon, targetLat, targetLon));
      //Serial.print("Distance Remaining:"); Serial.println((double)calc_dist(fLat, fLon, targetLat, targetLon));

    }
  //}

}

int calc_bearing(float flat1, float flon1, float flat2, float flon2)
{
  float calc;
  float bear_calc;

  float x = 69.1 * (flat2 - flat1); 
  float y = 69.1 * (flon2 - flon1) * cos(flat1/57.3);

  calc=atan2(y,x);

  bear_calc= degrees(calc);

  if(bear_calc<=1){
    bear_calc=360+bear_calc; 
  }
  return bear_calc;
}

void headingDirection(float heading) 
{
  //Use this part of the code to determine which way you need to go.
  //Remember: this is not the direction you are heading, it is the direction to the destination (north = forward).
  if ((heading > 348.75)||(heading < 11.25)) {
    Serial.println("  N");
    //Serial.println("Forward");
  }

  if ((heading >= 11.25)&&(heading < 33.75)) {
    Serial.println("NNE");
    //Serial.println("Go Right");
  }  

  if ((heading >= 33.75)&&(heading < 56.25)) {
    Serial.println(" NE");
    //Serial.println("Go Right");
  }

  if ((heading >= 56.25)&&(heading < 78.75)) {
    Serial.println("ENE");
    //Serial.println("Go Right");
  }

  if ((heading >= 78.75)&&(heading < 101.25)) {
    Serial.println("  E");
    //Serial.println("Go Right");
  }

  if ((heading >= 101.25)&&(heading < 123.75)) {
    Serial.println("ESE");
    //Serial.println("Go Right");
  }

  if ((heading >= 123.75)&&(heading < 146.25)) {
    Serial.println(" SE");
    //Serial.println("Go Right");
  }

  if ((heading >= 146.25)&&(heading < 168.75)) {
    Serial.println("SSE");
    //Serial.println("Go Right");
  }

  if ((heading >= 168.75)&&(heading < 191.25)) {
    Serial.println("  S");
    //Serial.println("Turn Around");
  }

  if ((heading >= 191.25)&&(heading < 213.75)) {
    Serial.println("SSW");
    //Serial.println("Go Left");
  }

  if ((heading >= 213.75)&&(heading < 236.25)) {
    Serial.println(" SW");
    //Serial.println("Go Left");
  }

  if ((heading >= 236.25)&&(heading < 258.75)) {
    Serial.println("WSW");
    //Serial.println("Go Left");
  }

  if ((heading >= 258.75)&&(heading < 281.25)) {
    Serial.println("  W");
    //Serial.println("Go Left");
  }

  if ((heading >= 281.25)&&(heading < 303.75)) {
    Serial.println("WNW");
    //Serial.println("Go Left");
  }

  if ((heading >= 303.75)&&(heading < 326.25)) {
    Serial.println(" NW");
    //Serial.println("Go Left");
  }

  if ((heading >= 326.25)&&(heading < 348.75)) {
    Serial.println("NWN");
    //Serial.println("Go Left");
  }
}

void headingDistance(float fDist)
{
  //Use this part of the code to determine how far you are away from the destination.
  //The total trip distance (from where you started) is divided into five trip segments.
 Serial.println(fDist);
 if ((fDist >= DESTINATION_DISTANCE)) { // You are now within 5 meters of your destination.
    //Serial.println("Trip Distance: 1");
    //Serial.println("Arrived at destination!");
    int i;
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, 0);
    }  
    strip.show();   // write all the pixels out
  }


  if ((fDist < DESTINATION_DISTANCE)) { // You are now within 5 meters of your destination.
    //Serial.println("Trip Distance: 0");
    //Serial.println("Arrived at destination!");
    breath();
  }

}

unsigned long calc_dist(float flat1, float flon1, float flat2, float flon2)
{
  float dist_calc=0;
  float dist_calc2=0;
  float diflat=0;
  float diflon=0;

  diflat=radians(flat2-flat1);
  flat1=radians(flat1);
  flat2=radians(flat2);
  diflon=radians((flon2)-(flon1));

  dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
  dist_calc2= cos(flat1);
  dist_calc2*=cos(flat2);
  dist_calc2*=sin(diflon/2.0);
  dist_calc2*=sin(diflon/2.0);
  dist_calc +=dist_calc2;

  dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));

  dist_calc*=6371000.0; //Converting to meters
  return dist_calc;
}

// Convert NMEA coordinate to decimal degrees
float decimalDegrees(float nmeaCoord, char dir) {
  uint16_t wholeDegrees = 0.01*nmeaCoord;
  int modifier = 1;

  if (dir == 'W' || dir == 'S') {
    modifier = -1;
  }

  return (wholeDegrees + (nmeaCoord - 100.0*wholeDegrees)/60.0) * modifier;
}

void breath()
{
  uniformBreathe(LED_Breathe_Table, BREATHE_TABLE_SIZE, BREATHE_UPDATE, 127, 127, 127);
}

void uniformBreathe(uint8_t* breatheTable, uint8_t breatheTableSize, uint16_t updatePeriod, uint16_t r, uint16_t g, uint16_t b)
{
  int i;

  uint8_t breatheBlu;

  if ((millis() - lastBreatheUpdate) > updatePeriod) {
    lastBreatheUpdate = millis();


    for (i=0; i < strip.numPixels(); i++) {
      breatheBlu = (b * breatheTable[breatheIndex]) / 256;
      strip.setPixelColor(i, 0, 0, breatheBlu);
    }
    strip.show();   

    breatheIndex++;
    if (breatheIndex > breatheTableSize) {
      breatheIndex = 0;
    }   
  }
}

What gets me the most is this part

void headingDistance(float fDist)
{
  //Use this part of the code to determine how far you are away from the destination.
  //The total trip distance (from where you started) is divided into five trip segments.
 Serial.println(fDist);
 if ((fDist >= DESTINATION_DISTANCE)) { // You are now within 5 meters of your destination.
    //Serial.println("Trip Distance: 1");
    //Serial.println("Arrived at destination!");
    int i;
    for (i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor(i, 0, 0, 0);
    }  
    strip.show();   // write all the pixels out
  }


  if ((fDist < DESTINATION_DISTANCE)) { // You are now within 5 meters of your destination.
    //Serial.println("Trip Distance: 0");
    //Serial.println("Arrived at destination!");
    breath();
  }

}

Since the comments say both are for within 5 meters but have opposite conditions and "fDist" isn't defined anywhere that I can see, I don't know which is for near or far or how to set a color to either one. I tried setting colors for the first if statement and they seemed to work, but I couldn't get it to work consistently for some reason.

3 Answers 3

3

I think others have addressed the need for you to modify the strip colors based on distance or bearing or... whatever you want to show.

Here's a NeoGPS version of that sketch. I think it may be easier to understand what's going on, and where you need to focus:

// Flora GPS + LED Pixel Code
//   Modified to use NeoGPS library.
//
// Adafruit Flora GPS module
//    ------> http://adafruit.com/products/1059
// Pick one up today at the Adafruit electronics shop
// and help support open source hardware & software! -ada

#include "Adafruit_NeoPixel.h"
#include <NMEAGPS.h>

NMEAGPS gps;
gps_fix fix;
#define gpsPort Serial1

const int NEOPIXEL_PIN = 6;

//----------------------------------------------------|
//                     WAYPOINTS                      |
//----------------------------------------------------|
// Please enter the latitude and longitude of your    |
// desired destination:                               |
NeoGPS::Location_t destination( 449950120L, -932289670L );
//  To be more accurate, these are degrees * 10000000 |
//----------------------------------------------------|

//--------------------------------------------------|
//                    DISTANCE                      |
//--------------------------------------------------|
//Please enter the distance (in meters) from your   |
//destination that you want your LEDs to light up:  |
const float DESTINATION_DISTANCE = 20.0;

//--------------------------------------------------|
// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel( 8, NEOPIXEL_PIN, NEO_GRB + NEO_KHZ800 );


uint8_t LED_Breathe_Table[]  =
  {
     80,  87,  95, 103, 112, 121, 131, 141, 151, 161, 172, 182, 192, 202, 211, 220,
    228, 236, 242, 247, 251, 254, 255, 255, 254, 251, 247, 242, 236, 228, 220, 211,
    202, 192, 182, 172, 161, 151, 141, 131, 121, 112, 103,  95,  87,  80,  73,  66,
     60,  55,  50,  45,  41,  38,  34,  31,  28,  26,  24,  22,  20,  20,  20,  20,
     20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,
     20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  20,  22,  24,  26,  28,
     31,  34,  38,  41,  45,  50,  55,  60,  66,  73
  };


#define BREATHE_TABLE_SIZE (sizeof(LED_Breathe_Table))
#define BREATHE_CYCLE      5000      /*breathe cycle in milliseconds*/
#define BREATHE_UPDATE     (BREATHE_CYCLE / BREATHE_TABLE_SIZE)
uint32_t lastBreatheUpdate = 0;
size_t   breatheIndex      = 0;

void setup()
{
  Serial.begin(115200);
  gpsPort.begin(9600);  // default baud rate for Adafruit MTK GPS's - some use 4800

  gps.send_P( &gpsPort, F("PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0") ); // RMC+GGA
  delay(100);
  gps.send_P( &gpsPort, F("PMTK220,1000") ); // 1Hz
  delay(100);
  gps.send_P( &gpsPort, F("PGCMD,33,0") ); // No antenna status

  strip.begin();
  strip.show();  // Update the strip, to start they are all 'off'
}


void loop()
{
  if (gps.available( gpsPort ) ) {
    fix = gps.read();

    const bool spoof = false; // change to true for spoofing
    if (spoof) {
      float t           = fix.dateTime.seconds / 4.0;  // 15 second cycle (60 / 4)
      float offsetAngle = (TWO_PI / 20.0) * t;         // 20 steps around circle
      float offsetDist  = 0.005 * t / NeoGPS::Location_t::EARTH_RADIUS_KM;
                           // 5m per second, 75m away or less. In RADIANS!
      fix.location = destination; // start here and step away
      fix.location.OffsetBy( offsetDist, offsetAngle );
    }


    //Serial.print( F("Location: ") );
    //Serial.print( fix.latitude(), 5 );
    //Serial.print( F(", ") );
    //Serial.print( fix.longitude(), 5 );

    if (fix.valid.location) {
      float distance = fix.location.DistanceKm( destination ) * 1000.0; // meters

      // Set below to true if you want your Flora to navigate to a
      //  certain destination.  Then modify the showDirection function.
      const bool showNavigationHints = true;

      if (showNavigationHints) {

        float bearing = fix.location.BearingToDegrees( destination );

        //  If we're moving fast enough, show the user which way to go, left or right.
        if (fix.valid.heading and (fix.valid.speed and fix.speed_kph() > 3.0)) {

          float courseCorrection = bearing - fix.heading();
          if (courseCorrection < 0.0)
            courseCorrection += 360.0;

          showDirection( courseCorrection );

        } else {
          // We're not moving very fast, show a compass direction.
          showDirection( bearing );
        }
      }

      showDistance( distance );
    }
  }
}


void showDirection( float bearing )
{
  //  Use this to show which way you need to go.
  //    Some times, it's an absolute compass direction.
  //    Other times, it's a course correction (left or right from current heading)

  const __FlashStringHelper *dir = compassDir( bearing );
  size_t dirLen = strlen_P( (const char *) dir );
  for (uint8_t i = 0; i < (3 - dirLen); i++)
    Serial.print( ' ' );
  Serial.println( dir );
}


void showDistance(float distance)
{
  Serial.println( distance );

  // Are we close enough?
  if (distance >= DESTINATION_DISTANCE) {
    // No.

    const uint8_t level = 0; // you might set this based on distance

    for (int i=0; i < strip.numPixels(); i++) {
      strip.setPixelColor( i, level, level, level );
    }
    strip.show();   // write all the pixels out

  } else {
    // Yes!
    // Serial.println("Arrived at destination!");
    breath();
  }

}


//------------------------------------------------------------
// This is copied from the NeoGPS NMEAaverage.ino example program

const char nCD  [] PROGMEM = "N";
const char nneCD[] PROGMEM = "NNE";
const char neCD [] PROGMEM = "NE";
const char eneCD[] PROGMEM = "ENE";
const char eCD  [] PROGMEM = "E";
const char eseCD[] PROGMEM = "ESE";
const char seCD [] PROGMEM = "SE";
const char sseCD[] PROGMEM = "SSE";
const char sCD  [] PROGMEM = "S";
const char sswCD[] PROGMEM = "SSW";
const char swCD [] PROGMEM = "SW";
const char wswCD[] PROGMEM = "WSW";
const char wCD  [] PROGMEM = "W";
const char wnwCD[] PROGMEM = "WNW";
const char nwCD [] PROGMEM = "NW";
const char nnwCD[] PROGMEM = "NNW";

const char * const dirStrings[] PROGMEM =
  { nCD, nneCD, neCD, eneCD, eCD, eseCD, seCD, sseCD,
    sCD, sswCD, swCD, wswCD, wCD, wnwCD, nwCD, nnwCD };

const __FlashStringHelper *compassDir( uint16_t bearing ) // degrees CW from N
{
  const int16_t directions    = sizeof(dirStrings)/sizeof(dirStrings[0]);
  const int16_t degreesPerDir = 360 / directions;
        int8_t  dir           = (bearing + degreesPerDir/2) / degreesPerDir;

  while (dir < 0)
    dir += directions;
  while (dir >= directions)
    dir -= directions;

  return (const __FlashStringHelper *) pgm_read_ptr( &dirStrings[ dir ] );

} // compassDir

//--------------------------


void uniformBreathe
  ( uint8_t *breatheTable, size_t breatheTableSize,
    uint16_t updatePeriod,
    uint16_t r, uint16_t g, uint16_t b )
{

  if ((millis() - lastBreatheUpdate) > updatePeriod) {
    lastBreatheUpdate = millis();

    for (int i=0; i < strip.numPixels(); i++) {
      uint8_t breatheBlu = (b * breatheTable[breatheIndex]) / 256;
      strip.setPixelColor( i, 0, 0, breatheBlu );
    }
    strip.show();

    breatheIndex++;
    if (breatheIndex > breatheTableSize) {
      breatheIndex = 0;
    }
  }
}

void breath()
{
  uniformBreathe(LED_Breathe_Table, BREATHE_TABLE_SIZE, BREATHE_UPDATE, 127, 127, 127);
}

I am always amazed by how much a little whitespace can improve the readability. Good variable and function names are also important. You are also being misled by bad comments. :P

I have added some location "spoofing" code that lets you test it without having to walk around. That spoofing section can be removed when you're done testing.

If you want to try it, NeoGPS is available from the Arduino Library Manager, under the menu Sketch-> Include Library-> Manage Libraries. BTW, this saves about 500 bytes of RAM.

To answer your original question, the showDistance and showDirection functions should be modified to do whatever you want.

1

Three comments:

  1. For someone who is new to Arduino and programming (or perhaps just new to C / C++), you have taken on a large challenge.
  2. It should be widely said (but it is not) that not all Arduino libraries are compatible with one another. And that it is up to the user (or community) to identify conflicts. As there are far too many combinations for any one person to test them all.
  3. You should ask only 1 question per post. I think you are most interested about programming different colors for a WS2812 LED. So we should not talk about GPS for now.

Most if not all Adafruit.com software source code are available free to the community at github.com. The Adafruit_NeoPixel github repository at github should contain all the source code for this project as well as several files of example code. In the example code is this segment of code that changes the LED color:

  switch(i){
    case 0: colorWipe(strip.Color(0, 0, 0), 50);    // Black/off
            break;
    case 1: colorWipe(strip.Color(255, 0, 0), 50);  // Red
            break;
    case 2: colorWipe(strip.Color(0, 255, 0), 50);  // Green
            break;
    case 3: colorWipe(strip.Color(0, 0, 255), 50);  // Blue
            break;
...removed some code for clarity...
    }
1

It is not really clear what your actual problem is. I will go with the following:

  1. How to set the colors of the RGB LEDs: You are already using the Adafruit_NeoPixel library. The definition of Adafruit_Neopixel strip shows, that you have a strip with 8 LEDs. In setup the sketch calls strip.begin() to initiate it. After that, everytime you want to change the color/state of any number of LEDs on the strip, you can set them one by one with strip.setPixelColor(), which takes the number of the LED and the color in RGB format as parameters. To make the LEDs show your changes you have to call strip.show(), which sends the data to the LEDs.
  2. What your sketch does: In the function headingDistance(float fDist) the colors of the LEDs are set according to the previously calculated distance. fDist comes as parameter to this function, so it is declared in the function header. The value comes from the function calc_dist(). Now you have 2 if-statements:
    1. The first one (if you are farer away from your target) will set the color of all LEDs to black (turning them off) by calling strip.setPixelColor(i, 0, 0, 0) for every LED and then calling strip.show(). So this is easy.
    2. The second one (if you are within the 5m radius) calls the function breath(). It will set the intensity of the blue color component for all LEDs dependent on it's parameter b and the time that passed since it set the LEDs color the last time (this is done with the millis()-if-statement). The function headingDistance() is called in loop(), whenever the fix variable of the GPS is set. Calling the breath() function (which is called from headingDistance()) very often results in a timed update of the LEDs every updatePeriod milliseconds. Refer to the BlinkWithoutDelay example in the Arduino IDE for this working principle. It leaves you time to do other things during the wait periods, because it is non-blocking. The breath() function is a more complex thing, than simply turning on and of the LEDs with the right color. If you just want to do that, replace it's call in headingDistance() with your own code, setting the LEDs like I explained above.

So:

one color to show when I'm in range of a a point, and another to show when I'm not.

Set the color values in RGB format in the first if-statement, to set for the case that your are not in the range of your GPS point. And replace the breath() function by a similar code with the color values in RGB format for the case, that you are within the range of your point.

If it still doesn't work, try if the original sketch works correctly. This would verify, that the measurement and calculation of the distance works as intended.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.