mikalhart / TinyGPS

A compact Arduino NMEA (GPS) parsing library
http://arduiniana.org
390 stars 257 forks source link

Anything inside void loop if statement does not work...WHYYYY? #24

Open Garretto123 opened 1 year ago

Garretto123 commented 1 year ago

Below is my code. The code is suppose to record speed (mph) and print it to the serial monitor using an arduino uno. Also, using the fastLED library, 50 leds will do a red wave. This code works ONLY when it is outside of the if statement "if (gps.encode(gpsSerial.read())) {". When the code is pasted inside, the led code works but very very slow. Is this a hardware issue? A bug in my code? I have no idea? Please help!

//library for GPS

include <TinyGPS++.h>

include

include

//Library for LED

include

// Set the LED parameters

define LED_PIN 3

define LED_TYPE WS2812B

define COLOR_ORDER GRB

define NUM_LEDS 50

float topSpeed = 50; CRGB leds[NUM_LEDS];

SoftwareSerial gpsSerial(8,9); //rx => pin 9 , tx => pin 8 TinyGPSPlus gps; float lattitude,longitude; float speed; unsigned long age;

void setup() {

gpsSerial.begin(9600); Serial.begin(9600); // Initialize the LED strip FastLED.addLeds<NEOPIXEL, LED_PIN>(leds, NUM_LEDS); // Set the brightness of the LED strip FastLED.setBrightness(150);

}

void loop() { while (gpsSerial.available()) { if (gps.encode(gpsSerial.read())) { if (gps.speed.isValid()) { speed = gps.speed.mph(); age = gps.speed.age(); Serial.print("Speed: "); Serial.print(speed); Serial.println(" mph"); //***Led functions inside static uint8_t hue = 0; // Starting hue value static uint8_t wavePosition = 0; // Current position of the wave

      // Calculate color for each LED based on wave position
      for (int i = 0; i < NUM_LEDS; i++) {
        uint8_t color = sin8(i * 8 + wavePosition); // Use sine function for color intensity
        leds[i] = CHSV(hue, 255, color); // Set color based on hue and intensity
      }
      wavePosition++; // Increment wave position for the next frame
      FastLED.show();
      delay(10); 

      //*******************************Led functions inside
    } else {
      Serial.println("No speed data");

      // Slowly pulses blue when "No speed data" is presented.
      uint8_t blue = 0; // Blue color value
      for (int i = 0; i < 255; i++) {
        blue = sin8(i);
        fill_solid(leds, NUM_LEDS, CRGB(0, 0, blue));
        FastLED.show();
        delay(10);
      }
      // Set all LEDs to off
      fill_solid(leds, NUM_LEDS, CRGB(0, 0, 0)); // Off
      FastLED.show();
    }
  }
}

}