Notifications
Clear all

Help For a trainee!

5 Posts
2 Users
0 Likes
399 Views
(@thephilnewman)
Member
Joined: 12 months ago
Posts: 222
Topic starter  

Hello, I am trying to do my homework for a chap who creates videos on YouTube called Paul McWhorter the episode is number 13, called "Understanding Arduino If Statements" he has set me a task to operate three LED's at different voltages. Hopefully there is someone on here who can tell me what I have done wrong in my code, because I have been staring at this for quite a while now and cannot see what is wrong, The error message I get when trying to upload the code is "Compilation error: 'High' was not declared in this scope" and I dont know what this means? I will try and attach the code as I have written it, please be gentle with me I am trying! If there is someone out there who can see what is wrong can they explain to me what I am doing wrong so I can learn from my mistake.

Thanks Phil

 

Please ignore this message I may have found the problem, uppercase/lowercase mistake!

 

 


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6939
 

First of all, why are you asking here, you should ask I Paul's venue. The error message means you did not declare the variable 'High' Since we normally spell these kinds of constants HIGH that may also be your error. Learning to code only takes a couple years, learning to debug much longer. 

I copied your code, compiled it, saw the first error, fixed it, saw the second error, fixed it, used Tools/Auto Format. Here is your finished work. 

I got it to compile at level ALL, I have NO idea if it works.

int myPin = A2;
int readVal;
float V2;
int dt = 250;
int redPin = 9;
int yellowPin = 8;
int greenPin = 7;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(myPin, INPUT);
  pinMode(redPin, OUTPUT);
  pinMode(yellowPin, OUTPUT);
  pinMode(greenPin, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  readVal = analogRead(myPin);
  V2 = (5. / 1023.) * readVal;
  Serial.print("Potentiometer Voltage is");
  Serial.println(V2);
  if (V2 > 0 && V2 < 3) {
    digitalWrite(greenPin, HIGH);
  }
  if (V2 < 4 && V2 > 3) {
    digitalWrite(yellowPin, HIGH);
  }
  if (V2 < 5 && V2 > 4) {
    digitalWrite(redPin, HIGH);
  }
  if (V2 > 3) {
    digitalWrite(greenPin, LOW);
  }
  if (V2 < 3 || V2 > 4) {
    digitalWrite(yellowPin, LOW);
  }
  if (V2 < 4) {
    digitalWrite(redPin, LOW);
  }
  delay(dt);
}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6939
 

@thephilnewman I took your code and did a lot of rework in terms of style, naming, and techniques showing some common bugs that new folks make. Who knows, maybe I made some errors. BTW, the IDE has a Tools menu item, 5th from the left, and then it has an Auto Format option as the first choice.

const int pinRED = 9;
const int pinYELLOW = 8;
const int pinGREEN = 7;
const int myPin = A2;

void setup() {
  Serial.begin(9600);
  pinMode(myPin, INPUT);
  pinMode(pinRED, OUTPUT);
  pinMode(pinYELLOW, OUTPUT);
  pinMode(pinGREEN, OUTPUT);
}

void loop() {
  const int timeDelay = 250;
  float analogVal = 0.0;

  analogVal = (5.0 / 1023.0) * analogRead(myPin);
  Serial.print("Potentiometer Voltage is ");
  Serial.println(analogVal);
  // inefficient and wrong 2 x
  if (analogVal > 0 && analogVal < 3) {
    digitalWrite(pinGREEN, HIGH);
  }
  if (analogVal < 4 && analogVal > 3) {
    digitalWrite(pinYELLOW, HIGH);
  }
  if (analogVal < 5 && analogVal > 4) {
    digitalWrite(pinRED, HIGH);
  }
  if (analogVal > 3) {
    digitalWrite(pinGREEN, LOW);
  }
  if (analogVal < 3 || analogVal > 4) {
    digitalWrite(pinYELLOW, LOW);
  }
  if (analogVal < 4) {
    digitalWrite(pinRED, LOW);
  }

  // More concise but still Wrong 2 X
  if (analogVal > 0 && analogVal < 3) digitalWrite(pinGREEN, HIGH);
  if (analogVal < 4 && analogVal > 3) digitalWrite(pinYELLOW, HIGH);
  if (analogVal < 5 && analogVal > 4) digitalWrite(pinRED, HIGH);
  if (analogVal > 3) digitalWrite(pinGREEN, LOW);
  if (analogVal < 3 || analogVal > 4) digitalWrite(pinYELLOW, LOW);
  if (analogVal < 4) digitalWrite(pinRED, LOW);

  // Better but still wrong 1 way (1 error fixed is once HIGH or LOW is found we can exit the test
  // also changed order of tests to read more naturally, i.e. >4 && <5 as oppsed to <5 && >4
  if (analogVal > 0 && analogVal < 3) digitalWrite(pinGREEN, HIGH);
  else if (analogVal > 3 && analogVal < 4) digitalWrite(pinYELLOW, HIGH);
  else if (analogVal > 4 && analogVal < 5) digitalWrite(pinRED, HIGH);
  if (analogVal > 3) digitalWrite(pinGREEN, LOW);
  else if (analogVal < 3 || analogVal > 4) digitalWrite(pinYELLOW, LOW);
  else if (analogVal < 4) digitalWrite(pinRED, LOW);

  // Compact / concise code still subtle errors
  // This is quite efficient, but still fails to test less than 0, greater than 5 and both 3 and 4 are tested twice
  if (analogVal >= 0 && analogVal <= 3) digitalWrite(pinGREEN, HIGH);
  else digitalWrite(pinGREEN, LOW);
  if (analogVal <= 4 && analogVal >= 3) digitalWrite(pinYELLOW, HIGH);
  else digitalWrite(pinYELLOW, LOW);
  if (analogVal <= 5 && analogVal >= 4) digitalWrite(pinRED, HIGH);
  else digitalWrite(pinRED, LOW);

  // OP did not explicity say if 0, 3, 4, 5 are valid and to which LED each belongs so some asumptions have been made such as
  // < 0 is error
  // GREEN is between >0 and 3
  // YELLOW is between >3 and <=4
  // and RED is between >4 and <= 5
  // >5 is error

  // Ternary operator and all erors covered
  if ((analogVal <= 0) || (analogVal >= 5)) {
    // ERROR
  } else {  // Each LED is set either ON or OFF dependig on the value of the analogRead
    ((analogVal > 0) && (analogVal <= 3)) ? digitalWrite(pinGREEN, HIGH) : digitalWrite(pinGREEN, LOW);
    ((analogVal > 3) && (analogVal <= 4)) ? digitalWrite(pinYELLOW, HIGH) : digitalWrite(pinYELLOW, LOW);
    ((analogVal > 4) && (analogVal < 5)) ? digitalWrite(pinRED, HIGH) : digitalWrite(pinRED, LOW);
  }

  // Once it is determined if 0 and 5 are valid values, then minor adjustments to this code are needed.
  //Similarly for 1, 2, 3, 4 as they need to be consciously placed in the appropritae buckets

  delay(timeDelay);
}

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@thephilnewman)
Member
Joined: 12 months ago
Posts: 222
Topic starter  

@zander not long after I wrote this I realised the upper/lower case issue and got the code to work, but this was a while after I first wrote it wrong and couldn't see the problem, hence I came back to the forum to try and delete the post but found I couldn't, so I wrote the note on the bottom that I had managed to sort the issue out. I wrote it as someone who is learning this and has only been on it for about three weeks, hence my sloppy method, hopefully I will get better. I wasn't aware that there was a forum on Paul McWhorter's site or else I would have gone there. I knew there would be someone on this forum that would be able to point me in the right direction hence coming here.

Thanks Phil


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6939
 

@thephilnewman As far as I know ALL YouTube channels have a forum like mechanism.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote