little help please on sketch

struggling a little finishing the sketch off

led light that comes on at 10kmph and stays on, Now need to add a button input and say if button is pressed then enable 10kmph cut off if not pressed then ignore 10kmph cut off

The sketch so far

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); ;    

#include <NMEAGPS.h>
#include <NeoSWSerial.h>
NeoSWSerial gpsSerial(6,7);  // gps on pins 6,7
NMEAGPS gps;

const int WARNING_LED_0 = 10;   // warning led on pin 10 over 
 


void setup()
{
  pinMode( WARNING_LED_0, OUTPUT );
  digitalWrite( WARNING_LED_0, LOW ); // (RELAY OFF IN NORMAL STATE)

  lcd.begin(16, 2);
  lcd.clear();
  lcd.print( F("Speed warning") ); // F macro saves RAM!
  gpsSerial.begin(9600);
  delay(2000);
  lcd.clear(); 
}

void printTime( NeoGPS::time_t & time )
{
  if (time.hours < 10)
    lcd.print( '0' );
  lcd.print( time.hours );
  if (time.minutes < 10)
    lcd.print( '0' );
  lcd.print( time.minutes );
  if (time.seconds < 10)
    lcd.print( '0' );
  lcd.print( time.seconds );

} // printTime


void displayGPS( gps_fix & fix )
{
  int kmh = 0;

  // Print speed
  lcd.setCursor(0,0);
  if (fix.valid.speed && (fix.spd.whole > 5)) {
    kmh = (fix.spd.whole * 185) / 100;
  }
  lcd.print( kmh );
  lcd.print( F(" km/h      ") );



   


if (kmh > 10) {
digitalWrite( WARNING_LED_0, HIGH ); }

    
 


 

  // Print time
  if (fix.valid.time) {
    printTime( fix.dateTime );
  }
} // displayGPS

void loop()
{

  
  while (gps.available( gpsSerial )) {  // if there is data coming from the GPS shield
    gps_fix fix = gps.read();           // get the complete fix structure
    displayGPS( fix );                  // Show pieces of the fix on the LCD
  }
}

How do you want the switch to act? Do you want the switch to toggle between 10kmph cut off enabled and 10kmph cut off disabled. I mean, one push enables and the next push disables?

yeah which ever is easiest

Create a boolean variable called (say) kmphLedEnabled and start with it false. Then every time you press the button the value changes between false and true.

In your other code have something like

if (kmphLedEnabled == true) {
   if (kmh > 10) {
       digitalWrite( WARNING_LED_0, HIGH ); 
    }
}
else {
     digitalWrite( WARNING_LED_0, LOW ); 
}

As a separate comment, if you use the AutoFormat tool to indent your code consistently it will make it much easier to understand.

...R

thank you

 #include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2); ;    

#include <NMEAGPS.h>
#include <NeoSWSerial.h>
NeoSWSerial gpsSerial(6,7);  // gps on pins 6,7
NMEAGPS gps;

const int WARNING_LED_0 = 10;   // warning led on pin 10 over 
const int kmphLedEnabled = 8; 
 


void setup()
{
  pinMode( WARNING_LED_0, OUTPUT );
  pinMode ( kmphLedEnabled, INPUT );
  digitalWrite( WARNING_LED_0, LOW ); // (RELAY OFF IN NORMAL STATE)

  

  lcd.begin(16, 2);
  lcd.clear();
  lcd.print( F("Speed warning") ); // F macro saves RAM!
  gpsSerial.begin(9600);
  delay(2000);
  lcd.clear(); 
}

void printTime( NeoGPS::time_t & time )
{
  if (time.hours < 10)
    lcd.print( '0' );
  lcd.print( time.hours );
  if (time.minutes < 10)
    lcd.print( '0' );
  lcd.print( time.minutes );
  if (time.seconds < 10)
    lcd.print( '0' );
  lcd.print( time.seconds );

} // printTime


void displayGPS( gps_fix & fix )
{
  int kmh = 0;

  // Print speed
  lcd.setCursor(0,0);
  if (fix.valid.speed && (fix.spd.whole > 5)) {
    kmh = (fix.spd.whole * 185) / 100;
  }
  lcd.print( kmh );
  lcd.print( F(" km/h      ") );



   


//if (kmh > 10) {
//digitalWrite( WARNING_LED_0, HIGH ); }

    
 if (kmphLedEnabled == true) {
   if (kmh > 10) {
       digitalWrite( WARNING_LED_0, HIGH );
    }
}
else {
     digitalWrite( WARNING_LED_0, LOW );



 

  // Print time
  if (fix.valid.time) {
    printTime( fix.dateTime );
  }
} // displayGPS

void loop()
{

  
  while (gps.available( gpsSerial )) {  // if there is data coming from the GPS shield
    gps_fix fix = gps.read();           // get the complete fix structure
    displayGPS( fix );                  // Show pieces of the fix on the LCD
  }
}

Any other help at all please

Help with what?

the code struggling implementing what robin mentioned bud

Arduino: 1.8.5 (Windows 10), Board: "Arduino Nano, ATmega328P"

C:\Users\shed1\Documents\Arduino\sketch_aug07a\sketch_aug07a.ino: In function 'void displayGPS(gps_fix&)':

sketch_aug07a:85: error: a function-definition is not allowed here before '{' token

{

^

sketch_aug07a:92: error: expected '}' at end of input

}

^

exit status 1
a function-definition is not allowed here before '{' token

Here is a sketch to demonstrate toggling the state of the 10kph enable (and the LED on pin 13) with a pushbutton using the state change detection method to sense a button press. Add the function to your code (outside of loop()) and call the checkButton() function each time through loop(). The button will be checked 20 times per second.
This code compiles and has been tested on my Uno, IDE ver 1.8.5.

// program to toggle the enable state using a momentary switch.

const int  buttonPin = 4;    // the pin that the pushbutton is attached to
const int ledPin = 13;

boolean buttonState = HIGH;         // current state of the button
boolean lastButtonState = HIGH;     // previous state of the button

boolean enableState = false;  // enable 10kph cutoff when true

void setup()
{
   // enable internal pullup on button pin
   pinMode(buttonPin, INPUT_PULLUP);
   // initialize the LED as an output
   pinMode(ledPin, OUTPUT);
}

void loop()
{
   checkButton();
}

void checkButton()
{
   static unsigned long timer = 0;
   unsigned long interval = 50; // check button 20 times per second
   if (millis() - timer >= interval)
   {
      timer = millis();
      buttonState = digitalRead(buttonPin);
      // compare the buttonState to its previous state
      if (buttonState != lastButtonState)
      {
         if (buttonState == LOW)
         {
            // if the current state is LOW then the button
            // went from off to on:
            digitalWrite(ledPin, !digitalRead(ledPin));  //toggle led
            enableState = !enableState;  // ***************toggle enable state
         }         
      }
      lastButtonState = buttonState;
   }
}

thank you shall try again

ktnch:
thank you shall try again

If you continue to have problems post the latest version of your program AND tell us in detail what happens when you run the program and what you want it to do that is different.

...R

will do quite new at this so its a matter of putting code and brackets in the right places

These links may help you to incorporate my function into your code (combining or merging codes).

https://arduino-info.wikispaces.com/CombiningArduinoSketches

http://www.thebox.myzen.co.uk/Tutorial/Merging_Code.html