So, I'm working on an R2D2 project that has run into a snag. When I through all of my test code together to make a full system test sketch I got a compile error. NONE of my other partial test sketches are throwing a compile error. And it's not flagging any of my code.
Thoughts?
Here's the code:
/*
* Author:
* Matt Tucker
* Date:
* 27APR2018
* Description:
* The combination of several individual test projects. The following code will test:
* 4 LEDs, 2 motors, 1 servo, and 1 speaker
* The code is to be executed on a Mega 2560 with a motor shield and sensor shield.
* Notes:
* Future iterations will include a Raspberry Pi running a Python script that communicates
* with the Mega 2560. The Python script will use OpenCV to evaluate images from a live
* video for faces, dogs, ect and then send commands to the Mega 2560 accordingly.
*/
//***Speaker***
#include <SD.h>
#define SDPIN 53
#include <TMRpcm.h>
TMRpcm tmrpcm; // @suppress("Type cannot be resolved")
int i = 1;
String file;
String type = ".wav";
//********END*****//
//***UltraSonic***
const int pingPin=A4;//UltraSonic Sensor Pin. let it be noted that const means constant
//********END*****//
//***LEDs***
const int ledPin1 = 34;
const int ledPin2 = 36;
const int ledPin3 = 38;
const int ledPin4 = 40;
//********END*****//
//***Motors_Servos***
#include <AFMotor.h>
#include <Servo.h>
AF_DCMotor motor1(1);
AF_DCMotor motor2(2);
Servo servo1;
const int INTPOST = servo1.read();
int angle;
//********END*****//
//***GeneralGlobalVariables***
long rndNum;
//********END*****//
void setup()
{
Serial.begin(9600);
randomSeed(analogRead(0));
//***Speaker***
tmrpcm.speakerPin = 46;
tmrpcm.setVolume(7);
if (!SD.begin(SDPIN))
{
Serial.println("Initialization Failed!");
return;
}
//********END*****//
//***Motors_Servo***
{
servo1.attach(9);
Serial.println("Servo's Recorded Initial Position: " + INTPOST);
Serial.println("Servo's Current Position: " + servo1.read());
}
//********END*****//
//***LEDs***
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
//********END*****//
//***StartUP***
tmrpcm.play("60.wav");
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(ledPin3, HIGH);
digitalWrite(ledPin4, HIGH);
delay(250);
//********END*****//
}
//***Motors***
void Forward()
{
motor1.setSpeed(200);
motor1.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(2000);
}
void VearLeft()
{
motor1.setSpeed(200);
motor1.run(RELEASE);
motor2.setSpeed(100);
motor2.run(RELEASE);
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(2000);
}
void VearRight()
{
motor1.setSpeed(100);
motor1.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
motor1.run(FORWARD);
motor2.run(FORWARD);
delay(2000);
}
void TurnLeft()
{
motor1.run(FORWARD);
motor2.run(BACKWARD);
delay(2000);
}
void TurnRight()
{
motor1.run(BACKWARD);
motor2.run(FORWARD);
delay(2000);
}
void Backward()
{
motor1.setSpeed(200);
motor1.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(2000);
}
void RevLeft()
{
motor1.setSpeed(100);
motor1.run(RELEASE);
motor2.setSpeed(200);
motor2.run(RELEASE);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(2000);
}
void RevRight()
{
motor1.setSpeed(200);
motor1.run(RELEASE);
motor2.setSpeed(100);
motor2.run(RELEASE);
motor1.run(BACKWARD);
motor2.run(BACKWARD);
delay(2000);
}
void Coast() //No dynamic breaking function, will have to calculate breaking algorithm
{
motor1.run(RELEASE);
motor2.run(RELEASE);
delay(2000);
}
/***Will calculate this by hand at later date***
void Stop()
{
}
*/
//********END*****//
//***Servo***
void Servo_Motor_Turn()
{
angle = random(1, 90);
rndNum = random();
if(rndNum%2 == 1)
{
servo1.write(90 - angle);
}
else
{
servo1.write(90 + angle);
}
}
void Servo_Motor_UnTurn()
{
servo1.write(INTPOST);
}
//********END*****//
//***UltraSonic***
long Sensor_loop()
{
long microseconds, inches;
pinMode(pingPin, OUTPUT);
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(5);
digitalWrite(pingPin, LOW);
pinMode(pingPin, INPUT);
microseconds=pulseIn(pingPin, HIGH);
//code to convert time to distance
inches= long (microseconds/74/2);
//code to print distance values
Serial.print(inches);
Serial.print("in, ");
return inches;
delay(100);
}
//********END*****//
//***LEDs***
void Blink()
{
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin3, LOW);
rndNum= random(20);
delay(rndNum*100);
digitalWrite(ledPin1, LOW);
rndNum= random(20);
delay(rndNum*100);
digitalWrite(ledPin4, LOW);
digitalWrite(ledPin2, HIGH);
rndNum= random(20);
delay(rndNum*100);
digitalWrite(ledPin3, HIGH);
rndNum= random(20);
delay(rndNum*100);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin4, HIGH);
}
//********END*****//
void loop()
{
//ALL***UltraSonic_Motors_Servo_Speaker_LEDs***ALL
long inches = Sensor_loop();
{
while(inches>6)
{
Forward();
Blink();
rndNum= random(20);
delay(rndNum*100);
}
Coast();
Blink();
file = String(i%60) + type;
tmrpcm.play(file.c_str());
Servo_Motor_Turn();
Backward();
delay(2000);
Coast();
Blink();
Servo_Motor_UnTurn();
delay(2000);
Blink();
Blink();
i++;
}
//********END*****//
}