About my program.
Hi, first year uni student in the UK, I'm new to posting on forums, but I thought I'd give it a shot because C++ has been frustrating me lately.
My program is a traffic lights system that should be able to switch between different modes automatically depending on different circumstances i.e input from a photo resistor and a range finder. I've written this flawlessly using functions, but I have decided to re-write the program to be more object oriented to meet my Uni's criteria.
I've only ever really dabbled with python, which I started to use at the start of my first year at Uni, so I don't have much experience programming, I can work my way around python though. I picked up C++ for this project around 2 to 3 days ago, and Classes have taken it out of me, I can hardly work with them even though I understand and have used them in python regularly (the idea's the same I think?) I've looked around for resources on the web and asked my lecturer for resources on this topic too, however they haven't been much help.
From what I gather I have a syntax error, (as well as a general organisation and functionality one - besides the point). If anyone could give me guidance, I'd very much appreciate it. Thanks in advance.
The Error
I'm using Tinkercad to emulate an Arduino (Uno R3), here Is the error I'm receiving.
20:1: error: expected unqualified-id before '{' token
50:1: error: expected unqualified-id before '{' token
68:1: error: expected unqualified-id before '{' token
125:1: error: expected unqualified-id before '{' token
In function 'void loop()':
164:7: error: expected unqualified-id before '.' token
exit status 1
The Code
Here is the code I have written using Sublime.
// Class Names.
#define rangeFinder rf;
#define lights light;
#define photoResistor resistor;
#define trafficLights check;
// Main Class Definitions.
class trafficLights
{
public:
void normal()
{
if (resistor.mode = 'over' && rangeFinder.distance > 10)
{
light.red('normal');
light.yellow('normal');
light.green('normal');
light.yellow('normal');
}
if (resistor.mode = 'over' && rangeFinder.distance < 10)
{
tone(5, 261);
light.yellow('blink');
noTone(5);
}
if (resistor.mode = 'under' && rangeFinder.distance > 10)
{
light.red('on');
}
if (resistor.mode = 'under' && rangeFinder.distance < 10)
{
light.allOff();
light.green('on');
}
}
};
class photoResistor
{
public:
char input = A0;
char mode{getLightIntensity(input)};
char getLightIntensity(input)
{
int reading = analogRead(input);
if (reading > 50)
{
return 'over';
} else {
return 'under';
}
}
};
class lights
{
public:
void red(char mode)
{
switch(mode){
case 'on':
digitalWrite(2, HIGH);
case 'off':
digitalWrite(2, LOW);
case 'normal':
digitalWrite(2, HIGH);
delay(2000);
digitalWrite(2, LOW);
}
}
void yellow(char mode)
{
switch(mode){
case 'on':
digitalWrite(3, HIGH);
case 'off':
digitalWrite(3, LOW);
case 'normal':
digitalWrite(3, HIGH);
delay(1000);
digitalWrite(3, LOW);
case 'blink':
for (int x = 0; x <= 10; x += 1){
digitalWrite(3, HIGH);
delay(100);
digitalWrite(3, LOW);
delay(100);
}
}
}
void green(char mode)
{
switch(mode){
case 'on':
digitalWrite(2, HIGH);
case 'off':
digitalWrite(2, LOW);
case 'normal':
digitalWrite(2, HIGH);
delay(2000);
digitalWrite(2, LOW);
}
}
void allOff()
{
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
}
};
class rangeFinder
{
public:
int trig = 7, echo = 6;
int distance{returnDistance()};
void reset()
{
digitalWrite(trig, LOW);
delay(2 / 1000);
digitalWrite(trig, HIGH);
delay(10 / 1000);
digitalWrite(trig, LOW);
}
int returnDistance()
{
reset();
int timeTaken = pulseIn(echo, HIGH);
return distance = timeTaken * 0.034 / 2;
}
};
// Setup.
void setup()
{
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, INPUT);
pinMode(7, OUTPUT);
pinMode('A0', INPUT);
}
// Loop.
void loop()
{
check.normal;
delay(10);
}
Final Comments
I'd appreciate any help I can get, I've been stuck on this issue for quite a while now and it is frustrating knowing that it's most likely me being blind.
Thanks!