user27581: I have a code for both a magnetometer, and a GPS, both written to give off just one reading at a time. I'm trying to make a sketch that is able to go back and forth between these two codes so I get both sets of data.
Here is the GPS Code:
#include <NMEAGPS.h>
#include <NeoSWSerial.h>

NeoSWSerial gpsSerial(3, 2);
NMEAGPS     GPS;
gps_fix     fix;

void setup()
{
Serial.begin(115200);
Serial.println( F("Run once!") ); // F macro saves RAM!

gpsSerial.begin(9600);
gpsSerial.println( F("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28") ); // RMC & GGA only
gpsSerial.println( F("$PMTK220,1000*1F") ); // 1Hz update rate

waitForFix();
printFix();
}

void loop()
{
}

void waitForFix()
{
uint8_t fixes = 0;

do {
if (GPS.available( gpsSerial )) {
  fix = GPS.read();
  fixes++;
}
} while ((fixes < 2) && !fix.valid.location);

} // waitForFix

void printFix() 
{
Serial.print( F("Time: ") );
if (fix.valid.time) {
if (fix.dateTime.hours < 10)
  Serial.print( '0' );
Serial.print(fix.dateTime.hours);
Serial.print(':');
if (fix.dateTime.hours < 10)
  Serial.print( '0' );
Serial.print(fix.dateTime.minutes);
Serial.print(':');
if (fix.dateTime.hours < 10)
  Serial.print( '0' );
Serial.print(fix.dateTime.seconds);
Serial.print('.');
if (fix.dateTime_cs < 10)
  Serial.print( '0' );
Serial.print(fix.dateTime_cs);
}
Serial.println();

Serial.print( F("Date: ") );
if (fix.valid.date) {
Serial.print(fix.dateTime.day);
Serial.print('/');
Serial.print(fix.dateTime.month);
Serial.print('/');
Serial.print( fix.dateTime.full_year() );
}
Serial.println();

Serial.print( F("Fix status: ") );
if (fix.valid.status)
Serial.print( (int) fix.status );
Serial.println();

if (fix.valid.location) {
Serial.print( F("Location: ") );
Serial.print( fix.latitude(), 4 );
Serial.print( F(", ") );
Serial.println(fix.longitude(), 4);
}

if (fix.valid.speed) {
Serial.print("Speed (knots): ");
Serial.println( fix.speed() );
}

if (fix.valid.heading) {
Serial.print( F("Angle: ") );
Serial.println( fix.heading() );
}

if (fix.valid.altitude) {
Serial.print( F("Altitude: ") );
Serial.println( fix.altitude() );
}

if (fix.valid.satellites) {
Serial.print( F("Satellites: ") );
Serial.println( fix.satellites );
}
}

Here is the Magnetometer Code:
#include <Wire.h> //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();

//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop(){

int x,y,z; //triple axis data

//Tell the HMC5883L where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();


//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6<=Wire.available()){

x = Wire.read()<<8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()<<8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()<<8; //Y msb
y |= Wire.read(); //Y lsb
}

//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print("  y: ");
Serial.print(y);
Serial.print("  z: ");
Serial.println(z);

delay(250);

while(1);

}

I don't know if this is even feasible, considering my lack of coding knowledge, but thanks for looking into it

user27157: The solution in short.
Although the link is very stupid, it shows very well what you have to do.
Pretty much make a code that combines both of them together. Put both setup in the new setup and carry on like that. Note that you have declared the serial monitor at 2 different baud rates so you can only pick one. Don't repeat previously declared things in your new code and you should be good to go.

Simple example:
Code 1:
const int buttonPin = 2;

 void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  buttonVal = digitalRead(buttonPin);
  Serial.println(buttonVal);
}

Code 2:
const int potPin = A0;

 void setup() {
  Serial.begin(115200);
}

void loop() {
  potVal = analogRead(potPin);
  Serial.println(potVal);
}

Fusionned code:
const int potPin = A0;
const int buttonPin = 2;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600); //go for the lowest baud rate for reliability
}

void loop() {
  buttonVal = digitalRead(buttonPin);
  Serial.print(buttonVal);

  Serial.print("  -  ")

  potVal = analogRead(potPin);
  Serial.println(potVal);
}


27581: <p>I have a code for both a magnetometer, and a GPS, both written to give off just one reading at a time. I'm trying to make a sketch that is able to go back and forth between these two codes so I get both sets of data.</p>

<p>Here is the GPS Code:</p>

<pre><code>#include &lt;NMEAGPS.h&gt;
#include &lt;NeoSWSerial.h&gt;

NeoSWSerial gpsSerial(3, 2);
NMEAGPS     GPS;
gps_fix     fix;

void setup()
{
Serial.begin(115200);
Serial.println( F("Run once!") ); // F macro saves RAM!

gpsSerial.begin(9600);
gpsSerial.println( F("$PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0*28") ); // RMC &amp; GGA only
gpsSerial.println( F("$PMTK220,1000*1F") ); // 1Hz update rate

waitForFix();
printFix();
}

void loop()
{
}

void waitForFix()
{
uint8_t fixes = 0;

do {
if (GPS.available( gpsSerial )) {
  fix = GPS.read();
  fixes++;
}
} while ((fixes &lt; 2) &amp;&amp; !fix.valid.location);

} // waitForFix

void printFix() 
{
Serial.print( F("Time: ") );
if (fix.valid.time) {
if (fix.dateTime.hours &lt; 10)
  Serial.print( '0' );
Serial.print(fix.dateTime.hours);
Serial.print(':');
if (fix.dateTime.hours &lt; 10)
  Serial.print( '0' );
Serial.print(fix.dateTime.minutes);
Serial.print(':');
if (fix.dateTime.hours &lt; 10)
  Serial.print( '0' );
Serial.print(fix.dateTime.seconds);
Serial.print('.');
if (fix.dateTime_cs &lt; 10)
  Serial.print( '0' );
Serial.print(fix.dateTime_cs);
}
Serial.println();

Serial.print( F("Date: ") );
if (fix.valid.date) {
Serial.print(fix.dateTime.day);
Serial.print('/');
Serial.print(fix.dateTime.month);
Serial.print('/');
Serial.print( fix.dateTime.full_year() );
}
Serial.println();

Serial.print( F("Fix status: ") );
if (fix.valid.status)
Serial.print( (int) fix.status );
Serial.println();

if (fix.valid.location) {
Serial.print( F("Location: ") );
Serial.print( fix.latitude(), 4 );
Serial.print( F(", ") );
Serial.println(fix.longitude(), 4);
}

if (fix.valid.speed) {
Serial.print("Speed (knots): ");
Serial.println( fix.speed() );
}

if (fix.valid.heading) {
Serial.print( F("Angle: ") );
Serial.println( fix.heading() );
}

if (fix.valid.altitude) {
Serial.print( F("Altitude: ") );
Serial.println( fix.altitude() );
}

if (fix.valid.satellites) {
Serial.print( F("Satellites: ") );
Serial.println( fix.satellites );
}
}
</code></pre>

<p>Here is the Magnetometer Code:</p>

<pre><code>#include &lt;Wire.h&gt; //I2C Arduino Library

#define address 0x1E //0011110b, I2C 7bit address of HMC5883

void setup(){
//Initialize Serial and I2C communications
Serial.begin(9600);
Wire.begin();

//Put the HMC5883 IC into the correct operating mode
Wire.beginTransmission(address); //open communication with HMC5883
Wire.write(0x02); //select mode register
Wire.write(0x00); //continuous measurement mode
Wire.endTransmission();
}
void loop(){

int x,y,z; //triple axis data

//Tell the HMC5883L where to begin reading data
Wire.beginTransmission(address);
Wire.write(0x03); //select register 3, X MSB register
Wire.endTransmission();


//Read data from each axis, 2 registers per axis
Wire.requestFrom(address, 6);
if(6&lt;=Wire.available()){

x = Wire.read()&lt;&lt;8; //X msb
x |= Wire.read(); //X lsb
z = Wire.read()&lt;&lt;8; //Z msb
z |= Wire.read(); //Z lsb
y = Wire.read()&lt;&lt;8; //Y msb
y |= Wire.read(); //Y lsb
}

//Print out values of each axis
Serial.print("x: ");
Serial.print(x);
Serial.print("  y: ");
Serial.print(y);
Serial.print("  z: ");
Serial.println(z);

delay(250);

while(1);

}
</code></pre>

<p>I don't know if this is even feasible, considering my lack of coding knowledge, but thanks for looking into it</p>
 27157: <p><a href="https://www.youtube.com/watch?v=1W3sslyiUfg" rel="nofollow noreferrer">The solution in short.</a></p>

<p>Although the link is <strong>very stupid</strong>, it shows very well what you have to do.</p>

<p>Pretty much make a code that combines both of them together. Put both setup in the new setup and carry on like that. Note that you have declared the serial monitor at 2 different baud rates so you can only pick one. Don't repeat previously declared things in your new code and you should be good to go.</p>

<hr>

<p>Simple example:</p>

<p>Code 1:</p>

<pre><code>const int buttonPin = 2;

 void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600);
}

void loop() {
  buttonVal = digitalRead(buttonPin);
  Serial.println(buttonVal);
}
</code></pre>

<p>Code 2:</p>

<pre><code>const int potPin = A0;

 void setup() {
  Serial.begin(115200);
}

void loop() {
  potVal = analogRead(potPin);
  Serial.println(potVal);
}
</code></pre>

<p>Fusionned code:</p>

<pre><code>const int potPin = A0;
const int buttonPin = 2;

void setup() {
  pinMode(buttonPin, INPUT);
  Serial.begin(9600); //go for the lowest baud rate for reliability
}

void loop() {
  buttonVal = digitalRead(buttonPin);
  Serial.print(buttonVal);

  Serial.print("  -  ")

  potVal = analogRead(potPin);
  Serial.println(potVal);
}
</code></pre>
