Arduino Mega - Code

I’ve got a car which is powered by an Arduino Mega. I am writing a code to travel through 4 different mazes. Now, I am new to this whole thing and I have just learnt C++. I am writing some code, but the software says that there are several mistakes. Would you mind looking at the code and explaining what to fix, ideas, etc. Below is the code. Here are a few things to keep in mind:

  • Ignore the code inside the user-defined functions Maze_A, B, C, and D.
  • There are 5 “tabs” {all 5 tabs are part of the same sketch} in my code, labeled Master_Controller, Maze_A, Maze_B, Maze_C, and Maze_D.

Tab 1-Master Controller:

#include <FastIO.h>
#include <I2CIO.h>
#include <LiquidCrystal_I2C_ByVac.h>
#include <LiquidCrystal_SI2C.h>
#include <LiquidCrystal_SR.h>
#include <LiquidCrystal_SR1W.h>
#include <LiquidCrystal_SR2W.h>
#include <LiquidCrystal_SR3W.h>
#include <SI2CIO.h>

#include <LCD.h>
#include <LiquidCrystal.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const int ENA = 13;
const int ENB = 12;

const int IN1 = 51;
const int IN2 = 50;
const int IN3 = 53;
const int IN4 = 52;

const int SW1 = 49;
const int SW2 = 48;
const int SW3 = 47;
const int SW4 = 46;

void setup() {
  pinMode (ENA, OUTPUT);
  pinMode (ENB, OUTPUT);
  
  pinMode (IN1, OUTPUT);
  pinMode (IN2, OUTPUT);
  pinMode (IN3, OUTPUT);
  pinMode (IN4, OUTPUT);

  pinMode (SW1, INPUT);
  pinMode (SW2, INPUT);
  pinMode (SW3, INPUT);
  pinMode (SW4, INPUT);
  
  lcd.begin (20,4);
}

Tab 2-Maze A

void maze_a() { 
  lcd.setCursor(7,1);
  lcd.print("Maze A");
  delay(3000);
}

if (SW1 == HIGH): {
  maze_a();
}

Tab 3-Maze B

void maze_b() { 
  lcd.setCursor(7,1);
  lcd.print("Maze B");
  delay(3000);
}

if (SW2 == HIGH): {
  maze_b();
}

Tab 4-Maze C

void maze_c() { 
  lcd.setCursor(7,1);
  lcd.print("Maze C");
  delay(3000);
}

if (SW3 == HIGH): {
  maze_c();
}

Tab 5-Maze D

void maze_d() { 
  lcd.setCursor(7,1);
  lcd.print("Maze D");
  delay(3000);
}

if (SW4 == HIGH): {
  maze_d();
}

System Errors:

In file included from /Users/laksh/Documents/Arduino/Master_Controller/Master_Controller.ino:6:0:
/Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5/LiquidCrystal_SR1W.h:157:1: warning: multi-line comment [-Wcomment]
 //             |    |      0.1uF            |     \
 ^
In file included from /Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5/LiquidCrystal_I2C_ByVac.h:49:0,
                 from /Users/laksh/Documents/Arduino/Master_Controller/Master_Controller.ino:3:
/Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5/LCD.h:89:40: warning: unused parameter 'uSec' [-Wunused-parameter]
 inline static void waitUsec ( uint16_t uSec )
                                        ^
/Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5/LCD.h:486:43: warning: unused parameter 'value' [-Wunused-parameter]
    virtual void setBacklightPin ( uint8_t value, t_backlighPol pol ) { };
                                           ^
/Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5/LCD.h:486:64: warning: unused parameter 'pol' [-Wunused-parameter]
    virtual void setBacklightPin ( uint8_t value, t_backlighPol pol ) { };
                                                                ^
/Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5/LCD.h:505:40: warning: unused parameter 'value' [-Wunused-parameter]
    virtual void setBacklight ( uint8_t value ) { };
                                        ^
Maze_A:8: error: expected unqualified-id before 'if'
 if (SW1 == HIGH): {
 ^
Maze_B:8: error: expected unqualified-id before 'if'
 if (SW2 == HIGH): {
 ^
Maze_C:8: error: expected unqualified-id before 'if'
 if (SW3 == HIGH): {
 ^
Maze_D:8: error: expected unqualified-id before 'if'
 if (SW4 == HIGH): {
 ^
Multiple libraries were found for "FastIO.h"
 Used: /Users/laksh/Documents/Arduino/libraries/Newliquidcrystal_1.3.5
 Not used: /Users/laksh/Documents/Arduino/libraries/NewliquidCrystal
 Not used: /Users/laksh/Documents/Arduino/libraries/NewliquidCrystal
 Not used: /Users/laksh/Documents/Arduino/libraries/NewliquidCrystal
 Not used: /Users/laksh/Documents/Arduino/libraries/NewliquidCrystal
exit status 1
expected unqualified-id before 'if'

Thank you for your contribution.

if (SW1 == HIGH): {I spy a colon. What is it doing there ?

UKHeliBob:
if (SW1 == HIGH): {I spy a colon. What is it doing there ?

I guess OP comes from the wondrous world of Python :slight_smile:

Then there are some warnings that need your attention, especially the “unused parameter” ones. Warnings won’t stop the code from compiling, but imply that you don’t fully understand what the functions do, and that the function probably behaves differently from what you expect.

UKHeli, are the colons a problem? It doesn't say so. Also, what is the "unqualified-id before the if" statement?

Wrong989: UKHeli, are the colons a problem? It doesn't say so. Also, what is the "unqualified-id before the if" statement?

That is probably the part where the compiler gets confused over the colons.

OK, I removed the colons and verified/compiled, but I got the exact same errors.

Hey guys thanks for all your help, I found the answer. I tried googling it, and I found another person who had the same problem (link at the end of this post), and the answer was doing something like this:

Maze Controller (new tab):

void loop() {
  if (SW1 == HIGH) {
    maze_a();
  }

  if (SW2 == HIGH) {
    maze_b();
  }

  if (SW3 == HIGH) {
    maze_c();
  }

  if (SW4 == HIGH) {
    maze_d();
  }
}

UKHeliBob, you were right. I got confused over Python. Thanks so much.