#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Define Pins Here
const int motorA = 12;
const int motorB = 13;
const int speedA = 3;
const int speedB = 11;
int ping1;
int ping2;
int ping3;
int ping4;
char pingdecision;
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Defines Stuff For NewPing Library
LiquidCrystal_I2C lcd(0x27,16,2); // Defines Stuff For LCD Library
Servo myservo;
void setup() {
lcd.init();
lcd.backlight();
pinMode(motorA, OUTPUT); // Set As Output
pinMode(motorB, OUTPUT); // Set As Output
pinMode(speedA, OUTPUT); // Set As Output
pinMode(speedB, OUTPUT); // Set As Output
myservo.attach(9); // Ataches Servo To Pin 9 MUST BE PWM!
}
void loop() {
check();
if (ping1 < 40) {
lcd.setCursor(0,0);
lcd.print("Object Detected");
delay(1000);
decision();
switch (pingdecision) {
case dforwards: // If Decision Returns Forwards Move Forwards
forwards();
lcd.setCursor(0,1);
lcd.print("Forwards");
break;
case dleft:
left();
break;
case dright:
right();
break;
}
}
void check(){
ping1 = sonar.ping_in();
return ping1;
}
void decision() {
servo_left();
ping2 = sonar.ping_in();
delay(100);
servo_center();
ping3 = sonar.ping_in();
delay(100);
servo_right();
ping4 = sonar.ping_in();
delay(100);
if (ping2 < ping3) {
pingdecision = dforwards;
}
else if (ping3 < ping2) {
pingdecision = dleft;
}
else {
pingdecision = dright;
}
}
return pingdecision;
}
void servo_left({
servo.write(50);
}
void servo_center(){
servo.write(75);
}
void servo_right(){
servo.write(100);
}
void forwards() {
digitalWrite(motorA, HIGH);
digitalWrite(motorB, LOW);
analogWrite(speedA, maxspeed);
analogWrite(speedB, maxspeed);
}
void backwards(){
digitalWrite(motorA, LOW);
digitalWrite(motorB, HIGH);
analogWrite(speedA, maxspeed);
analogWrite(speedB, maxspeed);
}
void left() {
digitalWrite(motorB, HIGH);
analogWrite(speedB, maxspeed);
}
void right() {
digitalWrite(motorA, HIGH);
analogWrite(speedA, maxspeed);
}
Errors:
sketch_apr12a.ino: In function 'void loop()':
sketch_apr12a:39: error: 'check' was not declared in this scope
sketch_apr12a:44: error: 'decision' was not declared in this scope
sketch_apr12a:46: error: 'dforwards' was not declared in this scope
sketch_apr12a:52: error: 'dleft' was not declared in this scope
sketch_apr12a:56: error: 'dright' was not declared in this scope
sketch_apr12a:62: error: a function-definition is not allowed here before '{' token
sketch_apr12a:67: error: a function-definition is not allowed here before '{' token
sketch_apr12a:126: error: expected `}' at end of input
sketch_apr12b.ino: In function 'void loop()':
sketch_apr12b:46: error: 'dforwards' was not declared in this scope
sketch_apr12b:52: error: 'dleft' was not declared in this scope
sketch_apr12b:56: error: 'dright' was not declared in this scope
sketch_apr12b.ino: In function 'void check()':
sketch_apr12b:65: error: return-statement with a value, in function returning 'void'
sketch_apr12b.ino: In function 'void decision()':
sketch_apr12b:69: error: 'servo_left' was not declared in this scope
sketch_apr12b:79: error: 'dforwards' was not declared in this scope
sketch_apr12b:83: error: 'dleft' was not declared in this scope
sketch_apr12b:86: error: 'dright' was not declared in this scope
sketch_apr12b:88: error: return-statement with a value, in function returning 'void'
sketch_apr12b.ino: At global scope:
sketch_apr12b:93: error: variable or field 'servo_left' declared void
sketch_apr12b:93: error: expected primary-expression before '{' token
#include <NewPing.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
// Define Pins Here
const int motorA = 12;
const int motorB = 13;
const int speedA = 3;
const int speedB = 11;
int ping1;
int ping2;
int ping3;
int ping4;
char pingdecision;
int pingdec;
#define TRIGGER_PIN 12
#define ECHO_PIN 11
#define MAX_DISTANCE 200
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // Defines Stuff For NewPing Library
LiquidCrystal_I2C lcd(0x27,16,2); // Defines Stuff For LCD Library
Servo myservo;
void setup() {
lcd.init();
lcd.backlight();
pinMode(motorA, OUTPUT); // Set As Output
pinMode(motorB, OUTPUT); // Set As Output
pinMode(speedA, OUTPUT); // Set As Output
pinMode(speedB, OUTPUT); // Set As Output
myservo.attach(9); // Ataches Servo To Pin 9 MUST BE PWM!
}
void loop() {
check();
if (pingdec == 1) {
lcd.setCursor(0,0);
lcd.print("Object Detected");
delay(1000);
decision();
switch (pingdecision) {
case 1: // If Decision Returns Forwards Move Forwards
forwards();
lcd.setCursor(0,1);
lcd.print("Forwards");
break;
case 2:
left();
break;
case 3:
right();
break;
default:
break;
}
}
else{
forwards();
}
}
void check(){
ping1 = sonar.ping_in();
if (ping1 < 40){
pingdec = 1;
}
else{
pingdec = 0;
}
return pingdec;
}
void decision() {
servo_left();
ping2 = sonar.ping_in();
delay(100);
servo_center();
ping3 = sonar.ping_in();
delay(100);
servo_right();
ping4 = sonar.ping_in();
delay(100);
if (ping2 < ping3) {
pingdecision = 1;
}
else
if (ping3 < ping2) {
pingdecision = 2;
}
else {
pingdecision = 3;
}
}
return pingdecision;
}
void servo_left({
servo.write(50);
}
void servo_center(){
servo.write(75);
}
void servo_right(){
servo.write(100);
}
void forwards() {
digitalWrite(motorA, HIGH);
digitalWrite(motorB, LOW);
analogWrite(speedA, maxspeed);
analogWrite(speedB, maxspeed);
}
void backwards(){
digitalWrite(motorA, LOW);
digitalWrite(motorB, HIGH);
analogWrite(speedA, maxspeed);
analogWrite(speedB, maxspeed);
}
void left() {
digitalWrite(motorB, HIGH);
analogWrite(speedB, maxspeed);
}
void right() {
digitalWrite(motorA, HIGH);
analogWrite(speedA, maxspeed);
}
I Get These Errors Now.
sketch_apr12b.ino: In function 'void check()':
sketch_apr12b:81: error: return-statement with a value, in function returning 'void'
sketch_apr12b.ino: At global scope:
sketch_apr12b:105: error: expected unqualified-id before 'return'
sketch_apr12b:106: error: expected declaration before '}' token
You are also attempting to return a value in a function you have declared as void. A void function isn't expecting to return a value. You are trying to return pingdec which is already global.
A good way to avoid mismatched brackets is to always type them out at the start. For example when I start a for statement I often type the following:
for()
{}
Then I hit ctrl-t for formatting, then I go back and put in code.