comudgen testing posting code cleanly

Post test messages here

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
KC4URI
 
Posts: 6
Joined: Thu Nov 13, 2014 11:55 pm

comudgen testing posting code cleanly

Post by KC4URI »

test

Code: Select all

[quote]
[color=#7E7E7E]//adafruit gps ultimate shield and 20x4 lcd I2C[/color]

#include <Adafruit_GPS.h>
#include <[color=#CC6600]SoftwareSerial[/color].h>
#include <[color=#CC6600]Wire[/color].h>
#include <[color=#CC6600]LiquidCrystal_I2C[/color].h>

[color=#CC6600]LiquidCrystal_I2C[/color] lcd(0X27);

[color=#7E7E7E]// Connect the GPS Power pin to 5V[/color]
[color=#7E7E7E]// Connect the GPS Ground pin to ground[/color]
[color=#7E7E7E]// If using software serial (sketch example default):[/color]
[color=#7E7E7E]//   Connect the GPS TX (transmit) pin to Digital 3[/color]
[color=#7E7E7E]//   Connect the GPS RX (receive) pin to Digital 2[/color]
[color=#7E7E7E]// If using hardware serial (e.g. Arduino Mega):[/color]
[color=#7E7E7E]//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3[/color]
[color=#7E7E7E]//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3[/color]

[color=#7E7E7E]// If you're using the Adafruit GPS shield, change [/color]
[color=#7E7E7E]// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);[/color]
[color=#7E7E7E]// and make sure the switch is set to SoftSerial[/color]

[color=#7E7E7E]// If using software serial, keep this line enabled[/color]
[color=#7E7E7E]// (you can change the pin numbers to match your wiring):[/color]
[color=#CC6600]SoftwareSerial[/color] mySerial(8, 7);

[color=#7E7E7E]// If using hardware serial (e.g. Arduino Mega), comment out the[/color]
[color=#7E7E7E]// above SoftwareSerial line, and enable this line instead[/color]
[color=#7E7E7E]// (you can change the Serial number to match your wiring):[/color]

[color=#7E7E7E]//HardwareSerial mySerial = Serial1;[/color]


Adafruit_GPS GPS(&mySerial);


[color=#7E7E7E]// Set GPSECHO to 'false' to turn off echoing the GPS data to the Serial console[/color]
[color=#7E7E7E]// Set to 'true' if you want to debug and listen to the raw GPS sentences. [/color]
#define GPSECHO  [color=#CC6600]true[/color]

[color=#7E7E7E]// this keeps track of whether we're using the interrupt[/color]
[color=#7E7E7E]// off by default![/color]
[color=#CC6600]boolean[/color] usingInterrupt = [color=#CC6600]false[/color];
[color=#CC6600]void[/color] useInterrupt([color=#CC6600]boolean[/color]); [color=#7E7E7E]// Func prototype keeps Arduino 0023 happy[/color]

[color=#CC6600]void[/color] [color=#CC6600][b]setup[/b][/color]()  
{    
 
lcd.[color=#CC6600]begin[/color] (20,4);
  
[color=#CC6600][b]Serial[/b][/color].[color=#CC6600]begin[/color](9600);
}


  [color=#7E7E7E]// when characters arrive over the serial port...[/color]
[color=#CC6600]if[/color]([color=#CC6600][b]Serial[/b][/color].[color=#CC6600]available[/color])  
  {
    [color=#7E7E7E]// wait a bit for the entire message to arrive[/color]
    [color=#CC6600]delay[/color](100);
    [color=#7E7E7E]// clear the screen[/color]
    lcd.[color=#CC6600]clear[/color]();
    [color=#7E7E7E]// read all the available characters[/color]
    [color=#CC6600]while[/color] ([color=#CC6600][b]Serial[/b][/color].[color=#CC6600]available[/color]() > 0) 
    {
      [color=#7E7E7E]// display each character to the LCD[/color]
      lcd.[color=#CC6600]write[/color]([color=#CC6600][b]Serial[/b][/color].[color=#CC6600]read[/color]());
    }
  }

  
  [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color]([color=#006699]"Adafruit GPS library basic test!"[/color]);

  [color=#7E7E7E]// 9600 NMEA is the default baud rate for Adafruit MTK GPS's- some use 4800[/color]
  GPS.[color=#CC6600]begin[/color](9600);
  
  [color=#7E7E7E]// uncomment this line to turn on RMC (recommended minimum) and GGA (fix data) including altitude[/color]
  GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);
  [color=#7E7E7E]// uncomment this line to turn on only the "minimum recommended" data[/color]
  [color=#7E7E7E]//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY);[/color]
  [color=#7E7E7E]// For parsing data, we don't suggest using anything but either RMC only or RMC+GGA since[/color]
  [color=#7E7E7E]// the parser doesn't care about other sentences at this time[/color]
  
  [color=#7E7E7E]// Set the update rate[/color]
  GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);   [color=#7E7E7E]// 1 Hz update rate[/color]
  [color=#7E7E7E]// For the parsing code to work nicely and have time to sort thru the data, and[/color]
  [color=#7E7E7E]// print it out we don't suggest using anything higher than 1 Hz[/color]

  [color=#7E7E7E]// Request updates on antenna status, comment out to keep quiet[/color]
  GPS.sendCommand(PGCMD_ANTENNA);

  [color=#7E7E7E]// the nice thing about this code is you can have a timer0 interrupt go off[/color]
  [color=#7E7E7E]// every 1 millisecond, and read data from the GPS for you. that makes the[/color]
  [color=#7E7E7E]// loop code a heck of a lot easier![/color]
  useInterrupt([color=#CC6600]true[/color]);

  [color=#CC6600]delay[/color](1000);
  [color=#7E7E7E]// Ask for firmware version[/color]
  mySerial.[color=#CC6600]println[/color](PMTK_Q_RELEASE);
}


[color=#7E7E7E]// Interrupt is called once a millisecond, looks for any new GPS data, and stores it[/color]
SIGNAL(TIMER0_COMPA_vect) {
  [color=#CC6600]char[/color] c = GPS.[color=#CC6600]read[/color]();
  [color=#7E7E7E]// if you want to debug, this is a good time to do it![/color]
#ifdef UDR0
  [color=#CC6600]if[/color] (GPSECHO)
    [color=#CC6600]if[/color] (c) UDR0 = c;  
    [color=#7E7E7E]// writing direct to UDR0 is much much faster than Serial.print [/color]
    [color=#7E7E7E]// but only one character can be written at a time. [/color]
#endif
}

[color=#CC6600]void[/color] useInterrupt([color=#CC6600]boolean[/color] v) {
  [color=#CC6600]if[/color] (v) {
    [color=#7E7E7E]// Timer0 is already used for millis() - we'll just interrupt somewhere[/color]
    [color=#7E7E7E]// in the middle and call the "Compare A" function above[/color]
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = [color=#CC6600]true[/color];
  } [color=#CC6600]else[/color] {
    [color=#7E7E7E]// do not call the interrupt function COMPA anymore[/color]
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = [color=#CC6600]false[/color];
  }
}

uint32_t timer = [color=#CC6600]millis[/color]();  

  [color=#CC6600]void[/color] [color=#CC6600][b]loop[/b][/color]()
  {  
  [color=#7E7E7E]// in case you are not using the interrupt above, you'll[/color]
  [color=#7E7E7E]// need to 'hand query' the GPS, not suggested :([/color]
  [color=#CC6600]if[/color] (! usingInterrupt) {
    [color=#7E7E7E]// read data from the GPS in the 'main loop'[/color]
    [color=#CC6600]char[/color] c = GPS.[color=#CC6600]read[/color]();
    [color=#7E7E7E]// if you want to debug, this is a good time to do it![/color]
    [color=#CC6600]if[/color] (GPSECHO)
      [color=#CC6600]if[/color] (c) [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](c);
  }
  
  [color=#7E7E7E]// if a sentence is received, we can check the checksum, parse it...[/color]
  [color=#CC6600]if[/color] (GPS.newNMEAreceived()) {
    [color=#7E7E7E]// a tricky thing here is if we print the NMEA sentence, or data[/color]
    [color=#7E7E7E]// we end up not listening and catching other sentences! [/color]
    [color=#7E7E7E]// so be very wary if using OUTPUT_ALLDATA and trytng to print out data[/color]
    [color=#7E7E7E]//Serial.println(GPS.lastNMEA());   // this also sets the newNMEAreceived() flag to false[/color]
  
    [color=#CC6600]if[/color] (!GPS.parse(GPS.lastNMEA()))   [color=#7E7E7E]// this also sets the newNMEAreceived() flag to false[/color]
      [color=#CC6600]return[/color];  [color=#7E7E7E]// we can fail to parse a sentence in which case we should just wait for another[/color]
  }

  [color=#7E7E7E]// if millis() or timer wraps around, we'll just reset it[/color]
  [color=#CC6600]if[/color] (timer > [color=#CC6600]millis[/color]())  timer = [color=#CC6600]millis[/color]();

  [color=#7E7E7E]// approximately every 2 seconds or so, print out the current stats[/color]
  [color=#CC6600]if[/color] ([color=#CC6600]millis[/color]() - timer > 2000) { 
    timer = [color=#CC6600]millis[/color](); [color=#7E7E7E]// reset the timer[/color]
    
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"\nTime: "[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.hour, [color=#006699]DEC[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]':'[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.minute, [color=#006699]DEC[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]':'[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.seconds, [color=#006699]DEC[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]'.'[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.milliseconds);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Date: "[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.day, [color=#006699]DEC[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]'/'[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.month, [color=#006699]DEC[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"/20"[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.year, [color=#006699]DEC[/color]);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Fix: "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](([color=#CC6600]int[/color])GPS.fix);
    [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]" quality: "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](([color=#CC6600]int[/color])GPS.fixquality); 
    [color=#CC6600]if[/color] (GPS.fix) {
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Location: "[/color]);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.latitude, 4); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.lat);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]", "[/color]); 
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.longitude, 4); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.lon);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Location (in degrees, works with Google Maps): "[/color]);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color](GPS.latitudeDegrees, 4);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]", "[/color]); 
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.longitudeDegrees, 4);
      
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Speed (knots): "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.[color=#CC6600]speed[/color]);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Angle: "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.angle);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Altitude: "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](GPS.[color=#CC6600]altitude[/color]);
      [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]print[/color]([color=#006699]"Satellites: "[/color]); [color=#CC6600][b]Serial[/b][/color].[color=#CC6600]println[/color](([color=#CC6600]int[/color])GPS.[color=#CC6600]satellites[/color]);
    }
  }
}

[/quote]
thanks

User avatar
KC4URI
 
Posts: 6
Joined: Thu Nov 13, 2014 11:55 pm

Re: comudgen testing posting code cleanly

Post by KC4URI »

//adafruit gps ultimate shield and 20x4 lcd I2C

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0X27);

// Connect the GPS Power pin to 5V
// Connect the GPS Ground pin to ground
// If using software serial (sketch example default):
//   Connect the GPS TX (transmit) pin to Digital 3
//   Connect the GPS RX (receive) pin to Digital 2
// If using hardware serial (e.g. Arduino Mega):
//   Connect the GPS TX (transmit) pin to Arduino RX1, RX2 or RX3
//   Connect the GPS RX (receive) pin to matching TX1, TX2 or TX3

// If you're using the Adafruit GPS shield, change 
// SoftwareSerial mySerial(3, 2); -> SoftwareSerial mySerial(8, 7);
// and make sure the switch is set to SoftSerial

// If using software serial, keep this line enabled
// (you can change the pin numbers to match your wiring):
SoftwareSerial mySerial(8, 7);

// If using hardware serial (e.g. Arduino Mega), comment out the
// above SoftwareSerial line, and enable this line instead
// (you can change the Serial number to match your wiring):

//HardwareSerial mySerial = Serial1;


Adafruit_GPS GPS(&mySerial);


// 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  true

// this keeps track of whether we're using the interrupt
// off by default!
boolean usingInterrupt = false;
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy

void setup()
{    
 
lcd.begin (20,4);
  
Serial.begin(9600);
}


  // when characters arrive over the serial port...
if(Serial.available)
  {
    // wait a bit for the entire message to arrive
    delay(100);
    // clear the screen
    lcd.clear();
    // read all the available characters
    while (Serial.available() > 0)
    {
      // display each character to the LCD
      lcd.write(Serial.read());
    }
  }

  
  Serial.println("Adafruit GPS library basic test!");

  // 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

  // Request updates on antenna status, comment out to keep quiet
  GPS.sendCommand(PGCMD_ANTENNA);

  // the nice thing about this code is you can have a timer0 interrupt go off
  // every 1 millisecond, and read data from the GPS for you. that makes the
  // loop code a heck of a lot easier!
  useInterrupt(true);

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


// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
  char c = GPS.read();
  // if you want to debug, this is a good time to do it!
#ifdef UDR0
  if (GPSECHO)
    if (c) UDR0 = c;
    // writing direct to UDR0 is much much faster than Serial.print
    // but only one character can be written at a time.
#endif
}

void useInterrupt(boolean v) {
  if (v) {
    // Timer0 is already used for millis() - we'll just interrupt somewhere
    // in the middle and call the "Compare A" function above
    OCR0A = 0xAF;
    TIMSK0 |= _BV(OCIE0A);
    usingInterrupt = true;
  } else {
    // do not call the interrupt function COMPA anymore
    TIMSK0 &= ~_BV(OCIE0A);
    usingInterrupt = false;
  }
}

uint32_t timer = millis();

  void loop()
  {  
  // in case you are not using the interrupt above, you'll
  // need to 'hand query' the GPS, not suggested :(
  if (! usingInterrupt) {
    // 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 millis() or timer wraps around, we'll just reset it
  if (timer > millis()) timer = millis();

  // approximately every 2 seconds or so, print out the current stats
  if (millis() - timer > 2000) {
    timer = millis(); // reset the timer
    
    Serial.print("\nTime: ");
    Serial.print(GPS.hour, DEC); Serial.print(':');
    Serial.print(GPS.minute, DEC); Serial.print(':');
    Serial.print(GPS.seconds, DEC); Serial.print('.');
    Serial.println(GPS.milliseconds);
    Serial.print("Date: ");
    Serial.print(GPS.day, DEC); Serial.print('/');
    Serial.print(GPS.month, DEC); Serial.print("/20");
    Serial.println(GPS.year, DEC);
    Serial.print("Fix: "); Serial.print((int)GPS.fix);
    Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
    if (GPS.fix) {
      Serial.print("Location: ");
      Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
      Serial.print(", ");
      Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
      Serial.print("Location (in degrees, works with Google Maps): ");
      Serial.print(GPS.latitudeDegrees, 4);
      Serial.print(", ");
      Serial.println(GPS.longitudeDegrees, 4);
      
      Serial.print("Speed (knots): "); Serial.println(GPS.speed);
      Serial.print("Angle: "); Serial.println(GPS.angle);
      Serial.print("Altitude: "); Serial.println(GPS.altitude);
      Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
    }
  }
}
thanks

User avatar
Franklin97355
 
Posts: 23911
Joined: Mon Apr 21, 2008 2:33 pm

Re: comudgen testing posting code cleanly

Post by Franklin97355 »

Quote tags add the color codes and then they get stuck when you run code tags. Copy your code between "

Code: Select all

 and [/code ]" tags (without the extra space which was needed to make them show up here) and try again.

Locked
Please be positive and constructive with your questions and comments.

Return to “Test Message Forum (closed)”