Expected constructor, destructor or type conversion etc

I have corrected many obvious mistakes in my coding but I'm unable to fathom this one. Any advice please.

int myPin = A2;
int readVal;
float V2;
int redPin = 4;
int yellowPin = 3;
int greenPin = 2;

void setup() {
  Serial.begin(9600);
  pinMode(myPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  readVal = analogRead(myPin);
  V2 = (5. / 1023.) * readVal;
  Serial.print("Potentiometer Voltage equals ");
  Serial.println(V2);
}

If (V2 < 3.0) {
  digitalWrite(greenPin, HIGH);
}
If (V2 > 3.0) {
  digitalWrite(greenPin, LOW);

}

If (V2 > 3.0 || < 4.0) {
  digitalWrite(yellowPin, HIGH);
}

If (V2 < 3.0 || > 4.0) {
  digitalWrite(yellowPin, LOW);
}

If (V2 > 4.0) {
  digitalWrite(redPin, HIGH);
}

If (V2 < 4.0) {
  digitalWrite(redPin, LOW);
}



...and here are the error messages;-
Arduino: 1.8.13 (Windows 10), Board: "Arduino Uno"

IF_Assignment:23:4: error: expected constructor, destructor, or type conversion before '(' token

If (V2 < 3.0) {

^

IF_Assignment:26:4: error: expected constructor, destructor, or type conversion before '(' token

If (V2 > 3.0) {

^

IF_Assignment:31:4: error: expected constructor, destructor, or type conversion before '(' token

If (V2 > 3.0 || < 4.0) {

^

IF_Assignment:35:4: error: expected constructor, destructor, or type conversion before '(' token

If (V2 < 3.0 || > 4.0) {

^

IF_Assignment:39:4: error: expected constructor, destructor, or type conversion before '(' token

If (V2 > 4.0) {

^

IF_Assignment:43:4: error: expected constructor, destructor, or type conversion before '(' token

If (V2 < 4.0) {

^

exit status 1

expected constructor, destructor, or type conversion before '(' token

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

None of those if statements are in a function. And it's if, not If. Case matters in C, but the difference is hard to read on the forum's font here.

First, all executable code must be inside of a function. Your "If" statements are inside no function.

Second, capitalization counts. It's 'if', not 'If'.

EDIT:
Too slow. What he said.

Thanks, I thought I'd already corrected the "IF" spelling!

  • do you mean I haven't placed the "if" inside {} when you say inside a function?

No

The series if if statements are simply not in a function

Where does the loop() function end ?

Here is a clue, highlighted by Auto Formatting the code in the IDE which shows the indentation of the code

void loop()
{
  readVal = analogRead(myPin);
  V2 = (5. / 1023.) * readVal;
  Serial.print("Potentiometer Voltage equals ");
  Serial.println(V2);
}

If (V2 < 3.0)
{
  digitalWrite(greenPin, HIGH);
}
If (V2 > 3.0)
{
  digitalWrite(greenPin, LOW);
}

If (V2 > 3.0 || < 4.0)
{
  digitalWrite(yellowPin, HIGH);
}
etc, etc

Apart from obvious wrong capitalisation of if, your comparison statements are also wrong. I suggest to take a crash course into C basics.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.