Making this function

i can only get this to do one of the “if” statements. the pots and PR don’t read against each other. and help would be great. it’s a basic class.

byte GreenLED=11; //Green LED uses digital pin 11
byte RedLED=12; //Red LED uses digital pin 12
byte potPin=A0; //uses analog pin 0, not digital pin 0
byte PhotoResistor=A1; //uses analog pin 1, not digital pin 1
byte Speaker=2; //Speaker uses digital pin 12
//The following are the freqs of notes C6 and B6
int C=1047;
int B=1976;

//The following are note lengths in ms
int Quarter=200; //a quarter note is 200ms+50ms gap between notes

void setup()
{
pinMode(Speaker, OUTPUT);
pinMode(GreenLED, OUTPUT);
pinMode(RedLED, OUTPUT);
pinMode(PhotoResistor, INPUT);
pinMode(potPin, INPUT);

int ScaleNotes={B,C,B,C,B,C,B,C,B,C,B,C,B,C,B,C,B,C,B,C};
int NoteLengths={Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter,Quarter};
for(int index=0;index<20;index++)
{
note(ScaleNotes[index],NoteLengths[index]);
}
}

void loop()
{
}

void note(int frequency, int NoteDuration) //plays a musical tone
{
if(analogRead(potPin)<=analogRead(PhotoResistor))
{
digitalWrite(GreenLED, HIGH);
digitalWrite(RedLED, LOW);

}
else if(analogRead(potPin)>=analogRead(PhotoResistor))
{
digitalWrite(GreenLED, LOW);
tone(Speaker, frequency);
digitalWrite(RedLED, HIGH);
delay(NoteDuration);
noTone(Speaker);
digitalWrite(RedLED, LOW);
}
}

Right, first things first.

Go and read the instructions, then go back and modify your post to mark up the code as such so we can examine it conveniently and accurately.

xsteiner22:
i can only get this to do one of the "if" statements. the pots and PR don't read against each other.

Can you explain what you mean here? What does it do? What did you want it to do?

Delta,
it's a microcontroller project for an intro class. i want it to, when in stand by mode, where the photoresistor has a higher read than the pots, to have the green LED on. when the light is blocked i want the piezo speaker and red LED to play the notes defined. I cant get it to function in this way. right now it only functions in one of those ways. its as if its not reading between the PR and Pots. sorry if my code has not been annotated its not finished yet.

xsteiner22:
sorry if my code has not been annotated its not finished yet.

That’s not what Paul__B meant. It’s not about comments in the code, its about how the code appears in your post. The code tags he is asking you to use (</> icon) make the code readable. Otherwise the forum software can mangle your code and make us think you have made mistakes that are not really there.

Compare this:
Hello
to this:
* *[i]Hello* *

See what we mean? Certain characters in a piece of code can be interpreted by the forum as formatting controls! The code tags stop that happening, and preserve your code indentation (you are correctly indenting your code, aren’t you?)
Paul

How do you have the pot and photoresistor wired? Try printing the values you read from them to serial to see if they are giving values that make sense. Maybe even write a new code that does nothing but read the pot and photoresistor and print those values to serial to see if you are reading them correctly.

The way you have it written now, if the read from the pot is less than the read from the photoresistor then it is going to run through writing the green led HIGH and the red led low 20 times really really fast (much faster than you can cover the PR or turn the pot) and then the program is over. It goes into the loop function and dutifully does nothing forever until the board is reset.