Hi everyone, i'm relatively new to Arduino and electronics in general. I am currently trying to use an Arduino Uno, a HC-SR04 ultrasonic sensor, a UBLOX 6M GPS and a L298N Dual Motor Controller in a school project.
I currently have all components functioning correctly giving serial monitor readings when my components are independent of each other. However, when I attempt to combine the code of these three components with each other i'm getting numerous errors. I've checked my wiring, all of my electronics are receiving 5 volts i've checked that the wiring is completely correct several times and I've changed around the code slightly to remove lines that I don't think were required.
I've tried to upload my code but when I do so I receive several errors that I don't really know the meaning of.
I have attached all my code below, as well as the errors that I received while attempting to upload. If anyone needs more info on my project i'm more than willing to share it.
Thanks in advance!
Ultrasonic:
/* Ultrasonic Sensor
* VCC to 5V
* GND to GND
* TRIG to 11
* ECHO to 4
*/
const int trigPin = 11;
const int echoPin = 4;
void setup() {
Serial.begin(9600);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
GPS:
/* GND to GND
* VCC to 5V
* RXD to 3
* TXD to 2
*/
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "TinyGPS++.h"
int fixed = 0;
int led = 13;
TinyGPSPlus gps;
SoftwareSerial SoftSerial(2, 3);
void setup()
{
pinMode(led, OUTPUT);
SoftSerial.begin(9600);
Serial.begin(9600);
}
void loop()
{
while (SoftSerial.available() > 0)
gps.encode(SoftSerial.read());
if (gps.location.isUpdated())
{
fixed = 1;
Serial.print("LAT="); Serial.print(gps.location.lat(), 6);
Serial.print("LNG="); Serial.println(gps.location.lng(), 6);
}
if (fixed == 0)
{
digitalWrite(led, HIGH);
delay(300);
digitalWrite(led, LOW);
delay(300);
}
if (gps.altitude.isUpdated())
{
Serial.print("Altitude:");
Serial.println(gps.altitude.meters());
}
}
Motor Driver:
// connect motor controller pins to Arduino digital pins
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup()
{
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoOne()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 200);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void demoTwo()
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
for (int i = 0; i < 256; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
for (int i = 255; i >= 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
demoOne();
delay(1000);
demoTwo();
delay(1000);
}
Combined Code:
/* Alfred Code
--- Ultrasonic ---
* VCC to 5V
* GND to GND
* TRIG to 11
* ECHO to 4
--- GPS ---
* VCC to 5V
* GND to GND
* RXD to 3
* TXD to 2
--- Motor Driver ---
* enA to 10;
* in1 to 9;
* in2 to 8;
* enB to 5;
* in3 to 7;
* in4 to 6;
*/
// GPS
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include "TinyGPS++.h"
int fixed = 0;
int led = 13;
TinyGPSPlus gps;
SoftwareSerial SoftSerial(2, 3);
// Ultrasonic
const int trigPin = 11;
const int echoPin = 4;
// Motor Driver
// motor one
int enA = 10;
int in1 = 9;
int in2 = 8;
// motor two
int enB = 5;
int in3 = 7;
int in4 = 6;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
SoftSerial.begin(9600);
pinMode(enA, OUTPUT);
pinMode(enB, OUTPUT);
pinMode(in1, OUTPUT);
pinMode(in2, OUTPUT);
pinMode(in3, OUTPUT);
pinMode(in4, OUTPUT);
}
void demoOne()
{
digitalWrite(in1, HIGH);
digitalWrite(in2, LOW);
analogWrite(enA, 200);
digitalWrite(in3, HIGH);
digitalWrite(in4, LOW);
analogWrite(enB, 200);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
delay(2000);
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void demoTwo()
{
digitalWrite(in1, LOW);
digitalWrite(in2, HIGH);
digitalWrite(in3, LOW);
digitalWrite(in4, HIGH);
for (int i = 0; i < 256; i++)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
for (int i = 255; i >= 0; --i)
{
analogWrite(enA, i);
analogWrite(enB, i);
delay(20);
}
digitalWrite(in1, LOW);
digitalWrite(in2, LOW);
digitalWrite(in3, LOW);
digitalWrite(in4, LOW);
}
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
while (SoftSerial.available() > 0)
gps.encode(SoftSerial.read());
if (gps.location.isUpdated())
{
fixed = 1;
Serial.print("LAT="); Serial.print(gps.location.lat(), 6);
Serial.print("LNG="); Serial.println(gps.location.lng(), 6);
}
if (fixed == 0)
{
digitalWrite(led, HIGH);
delay(300);
digitalWrite(led, LOW);
delay(300);
}
if (gps.altitude.isUpdated())
{
Serial.print("Altitude:");
Serial.println(gps.altitude.meters());
}
demoOne();
delay(1000);
demoTwo();
delay(1000);
Errors:
Arduino: 1.8.3 (Mac OS X), Board: "Arduino/Genuino Uno"
In function 'void loop()':
Alfred_Final:121: error: 'microsecondsToInches' was not declared in this scope
inches = microsecondsToInches(duration);
^
Alfred_Final:122: error: 'microsecondsToCentimeters' was not declared in this scope
cm = microsecondsToCentimeters(duration);
^
Alfred_Final:133: error: a function-definition is not allowed here before '{' token
{
^
Alfred_Final:138: error: a function-definition is not allowed here before '{' token
{
^
Alfred_Final:167: error: expected '}' at end of input
delay(1000);
^
exit status 1
'microsecondsToInches' was not declared in this scope