Running a code only once

I have this code for a GPS, and I need it to only loop once. I am making a separate code to go between both this GPS code, and a magnetometer, but this code must loop only once for this to happen.

Here’s the code:

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

SoftwareSerial mySerial(3, 2);

Adafruit_GPS GPS(&mySerial);

#define GPSECHO true

boolean usingInterrupt = false;
void useInterrupt(boolean);

void setup()
{

Serial.begin(115200);
Serial.println(“Adafruit GPS library basic test!”);

GPS.begin(9600);

GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

GPS.sendCommand(PGCMD_ANTENNA);

useInterrupt(true);

delay(1000);

mySerial.println(PMTK_Q_RELEASE);
}

SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();

#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;

#endif
}

void useInterrupt(boolean v) {
if (v) {

OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {

TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}

uint32_t timer = millis();
void loop() // run over and over again
{

if (! usingInterrupt) {

char c = GPS.read();

if (GPSECHO)
if (c) Serial.print(c);
}

if (GPS.newNMEAreceived()) {

if (!GPS.parse(GPS.lastNMEA()))
return;

if (timer > millis()) timer = millis();

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);
}

}

}
}

Please use code tags when posting code.

Stuff that you only want to run once is usually placed in setup().

What should happen after it runs?

Whenever that is done, the error “expected unqualified-id before ‘{’ token” appears for the { above

if (! usingInterrupt) {

why is this happening, and how do i fix it?

This is what the code looks like when i move it to void setup:

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

SoftwareSerial mySerial(3, 2);

Adafruit_GPS GPS(&mySerial);

#define GPSECHO true

boolean usingInterrupt = false;
void useInterrupt(boolean);

void setup()
{

Serial.begin(115200);
Serial.println(“Adafruit GPS library basic test!”);

GPS.begin(9600);

GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA);

GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ);

GPS.sendCommand(PGCMD_ANTENNA);

useInterrupt(true);

delay(1000);

mySerial.println(PMTK_Q_RELEASE);

}

SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();

#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;

#endif
}

void useInterrupt(boolean v) {
if (v) {

OCR0A = 0xAF;
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
} else {

TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}

uint32_t timer = millis();

{ This is where the error is occuring

if (! usingInterrupt) {

char c = GPS.read();

if (GPSECHO)
if (c) Serial.print(c);
}

if (GPS.newNMEAreceived()) {

if (!GPS.parse(GPS.lastNMEA()))
return;

if (timer > millis()) timer = millis();

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);
}

}

}
}

void loop() // run over and over again

And when is runs, it should tell me the GPS data from the adafruit module

Please learn how to use code tags! What is the stray { doing here:

{            This is where the error is occuring

int hasRan = 0;

void setup(){

//setup

}

void loop(){

if (hasRan == 0){

//your code here

hasRan = 1;

} }

@leotakacs, if you really want to get help here, you need to start listening to the suggestions of those who reply. You've now been asked twice to use code tags, and totally ignored it.

You really should have read How to use this forum before posting. (Especially item #7.)

ie Your code and any error messages should always be placed between [code]code tags[/code]. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis, and often also corrupts the code with italics or smilies.

It's still not too late to edit your post(s) and do this. You'll make potential helpers much happier. :)

Only once ever? After you upload this code for the first time, it does this thing, and then it never does it again?

Only once every time the arduino is turned on?

Only once every time you push a button?

Only once, at intervals of one hour?

OldSteve: @leotakacs, if you really want to get help here, you need to start listening to the suggestions of those who reply. You've now been asked twice to use code tags, and totally ignored it.

leotakacs is not the OP; so this might have been an attempt to help;)

sterretje: leotakacs is not the OP; so this might have been an attempt to help;)

Oops. My bad.

Sorry leotakacs.

I take back half of what I said. (Still should have been in code tags, though.)