Trouble with If()/Else()/Else if() and For()

Hi everybody, I am having a (several actually but this is just about one) problem with some code I am writing for my guitar tuner project.

Basically until I have programmed in my FFT (which is going to kill me) I have been just using random values of x and then using those values to identify the note and then determine if it is sharp or flat and so on. I have managed to get my LCD to display the string being played but I can’t as of yet also get it to display whether it is sharp or flat (etc.) without making my code ridiculously long. Below is my code and I’ll explain how I managed to get it to work and my problem with it.

#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
float StrI[] = {285.43, 329.63, 369.99};
float StrII[] = {220.00, 246.94, 285.42};
float StrIII[] = {169.81, 196.00, 219.99};
float StrIV[] = {127.47, 146.83, 169.61};
float StrV[] = {95.25, 110.00, 127.57};
float StrVI[] = {65.406, 82.407, 95.15};
float Strings[] = {73.416, 95.00, 128.00, 170.00, 220.00, 285.00, 392.00};
float a = Strings[0];
float b = Strings[1];
float c = Strings[2];
float d = Strings[3];
float e = Strings[4];
float f = Strings[5];
float g = Strings[6];
int count = 1;
float x = random(30,400);
float h = 0;
float i = 0;
float j = 0;

void setup(){
  Serial.begin(9600);
   pinMode(13, OUTPUT);  

  lcd.init();
   lcd.clear();
  lcd.printIn("Guitar Tuner");
  delay(2000);
  lcd.clear();
  lcd.printIn("By Liam Simpson");
  delay(2000);
  lcd.clear();
  lcd.printIn("E,A,D,G,b,e?");
  delay(10000);
  lcd.clear();
  }

void loop(){                                  /* the For() functions determine which string is being played and therefor defining which string array and h,i    
                                                  and j which are used later to determine sharp, flat or "Bingo!" */
  for(x>=a && x<=b;x<=100;) {
    float h = StrVI[1];
    float i = StrVI[2];
    float j = StrVI[0];
    lcd.printIn("E");
    delay(5000);
  }
  for(x>=b && x<c;x<=130;) {
    float h = StrV[1];
    float i = StrV[2];
    float j = StrV[0];
    int count = 1;
    lcd.printIn("A");
    delay(5000);
}
  for(x>=c && x<d;x<=175.00;) {
    float h = StrIV[1];
    float i = StrIV[2];
    float j = StrIV[0];
    lcd.printIn("D");
    delay(5000);
  }
  for(x>=d && x<e;x<=225.00;) {
    float h = StrIII[1];
    float i = StrIII[2];
    float j = StrIII[0];
    lcd.printIn("G");
    delay(5000);
    lcd.clear();
  }
  for(x>=e && x<f;x<=290.00;) {
    float h = StrII[1];
    float i = StrII[2];
    float j = StrII[0];
    lcd.printIn("b"); 
    delay(5000);
  }
  for(x>=f && x<g;x<=395.00;) {
    float h = StrI[1];
    float i = StrI[2];
    float j = StrI[0];
    lcd.printIn("e");
    delay(5000);
  }
  lcd.cursorTo(2,0);
  
  if(x>h && x<i) {         /*The If/else/else if functions use the previously determined h,i and j values to determine whether the pitch is flat or sharp 
                                   etc. */
    lcd.printIn("Sharp");  
  }
  else if(x<h && x>j) {
    lcd.printIn("Flat");  
  }
  else if(x==h){
    lcd.printIn("Bingo!"); 
  }
  else {
    count++;count>=0;
    lcd.printIn("Undetected");  
  }  
  delay(1000);
  lcd.clear();
 }

The only way I’ve managed to make it work so far is to add the following code into every piece of For() code.

 if(x>h && x<i) {
    lcd.printIn("Sharp");  
  }
  else if(x<h && x>j) {
    lcd.printIn("Flat");  
  }
  else if(x==h){
    lcd.printIn("Bingo!");  
  }
  else {
    count++;count>=0;
    lcd.printIn("Undetected");  
  }  
  delay(1000);
  lcd.clear();

code into every piece of For() code.

Please help, this is supposed to be the easy part of my code, I can’t imagine how i’m going to do the FFT!

Thanks

  for(x>=a && x<=b;x<=100;) {
    float h = StrVI[1];
    float i = StrVI[2];
    float j = StrVI[0];
    lcd.printIn("E");
    delay(5000);
  }

What does this portion of code and those like it do ? What is it meant to do ?

This section identifies whether the variable x is string 6 and if it is, then storing the values of the string 6 array into h, i & j and then later using those array values to determine if the variable x is higher or lower than the ideal value.

I promise that I am not winding you up, but can you explain how it does what you say ? I am particularly intrigued by
for(x>=a && x<=b;x<=100;)How many times does the for loop execute, if at all, and why ?

The for-loop is used like this:

int i;
for ( i=12; i<100; i++)

You have a condition in the first part. That is not okay.
Perhaps you want a if-statement ?

if( x > 100.0 && x < 200.0)

Spoilsport.
It looks to me as like it should be an if too but I was looking forward to the explanation of the for loop in the program.

What happens if the measured note is Not C# but say 20 cycles above of below. My viewpoint is that C# represents a specific frequency and the desired frequency should be displayed as well as the error… High or low of C#. I don’t see anything that displays the actual tuning and the error + or -…
So that the direction to turn the peg is known as well.
There is in this piece of code another error…?

for(x>=a && x<=b;x<=100;) {
float h = StrVI[1];
float i = StrVI[2];
float j = StrVI[0];
lcd.printIn(“E”); <— lcd.print"I"n, shouldn’t this be lcd.println? and how did this get past the compiler?
delay(5000); I’m assuming a blocking delay is appropriate here. Where the controller must wait 5 seconds doing nothing?

Doc

Ok so I’ve changed it as advised and it is now looping so thank you for that. My problem is that now it is only showing the “else” line (the text “Undetectable”) on line two. So my code is now:

#include <LCD4Bit_mod.h>
LCD4Bit_mod lcd = LCD4Bit_mod(2);
float StrI[] = {285.43, 329.63, 369.99};
float StrII[] = {220.00, 246.94, 285.42};
float StrIII[] = {169.81, 196.00, 219.99};
float StrIV[] = {127.47, 146.83, 169.61};
float StrV[] = {95.25, 110.00, 127.57};
float StrVI[] = {65.406, 82.407, 95.15};
float Strings[] = {73.416, 95.00, 128.00, 170.00, 220.00, 285.00, 392.00};
float a = Strings[0];
float b = Strings[1];
float c = Strings[2];
float d = Strings[3];
float e = Strings[4];
float f = Strings[5];
float g = Strings[6];
int count = 1;
const float x = random(30,400);
float h = 0;
float i = 0;
float j = 0;

void setup(){
  Serial.begin(9600);
   pinMode(13, OUTPUT);  
  lcd.init();
   lcd.clear();
  lcd.printIn("Guitar Tuner");
  delay(1000);
  lcd.clear();
  lcd.printIn("By Liam Simpson");
  delay(1000);
  lcd.clear();
  lcd.printIn("E,A,D,G,b,e?");
  delay(3000);
  lcd.clear();
  }
void loop(){
  float x = random(30,400);
  Serial.println(x);
  if(x > Strings[0] && x < Strings[1]) {
    float h = StrVI[1];
    float i = StrVI[2];
    float j = StrVI[0];
    lcd.cursorTo(1,0);
    lcd.printIn("E");
  }
  if(x > Strings[1] && x < Strings[2]) {
    float h = StrV[1];
    float i = StrV[2];
    float j = StrV[0];
    lcd.cursorTo(1,2);
    lcd.printIn("A");
}
  if(x > Strings[2] && x < Strings[3]) {
    float h = StrIV[1];
    float i = StrIV[2];
    float j = StrIV[0];
    lcd.cursorTo(1,4);
    lcd.printIn("D");
  }
  if(x > Strings[3] && x < Strings[4]) {
    float h = StrIII[1];
    float i = StrIII[2];
    float j = StrIII[0];
    lcd.cursorTo(1,6);
    lcd.printIn("G");
  }
  if(x > Strings[4] && x < Strings[5]) {
    float h = StrII[1];
    float i = StrII[2];
    float j = StrII[0];
    lcd.cursorTo(1,8);
    lcd.printIn("b");    
  }
  if(x > Strings[5] && x < Strings[6]) {
    float h = StrI[1];
    float i = StrI[2];
    float j = StrI[0];
    lcd.cursorTo(1,10);
    lcd.printIn("e");
  }
  if(x>h && x<i) {
    lcd.cursorTo(2,0);
    lcd.printIn("Sharp");  
  }
  else if(x<h && x>j) {
    lcd.cursorTo(2,0);
    lcd.printIn("Flat");  
  }
  else if(x==h){
    lcd.cursorTo(2,0);
    lcd.printIn("Bingo!");  
  }
  else{
    lcd.cursorTo(2,0);
    lcd.printIn("Undetectable");
  }
  delay(2000);
  lcd.clear();
 }

And Docedison, it is print-I-n because of the LCD library i think.

And the reason for the previous code is that i’m very very tired.