(n00b) multiple ifs, general programming mishaps, and learning to program

Name is Mitch, trying to advance my skills with arduino before attempting larger project (raspberry pi and arduino robot)

making a simple Light Dependant Resistor(LDR) operated synth; my intent is to simply convert the last number in the analog value of the ldr into one of 10 notes in the d minor scale. board is wired, and, to the best of my ability, my code is written, but I can see I am making plenty of mistakes in my coding; i need to learn the intricacies of arduino, and i am already past the absolute beginner stage, and want to progress into the amateur stage. please help me find the glaring errors in my programming so I may improve!

/*
  Education through Hard Programming
  LDR Based SYNTHESIZER
  plays basic scales based on feedback from LDR
  Written By Mitchell Cottew
  */
#include "pitches.h"

int LDR = A5;
int out = pin3;
int LDRVal = 0;
void setup() {
// digital pin as output;
// analog pin as input;
// maybe i should do serialout too just so i know whats goin on;
pinMode(out,OUTPUT);
pinMode(LDR,INPUT);
}

void loop() {

  int LDRVal = analogRead(LDR);
  int musicValue = LDRVal % 10;
  //read LDR and turn it into simple number
  
  if ((musicValue) == 0);
  {tone(294)};
  if ((musicValue) == 1);
  {tone(330)};
  if ((musicValue) == 2);
  {tone(349)};
  if ((musicValue) == 3);
  {tone(392)};
  if ((musicValue) == 4);
  {tone(440)};
  if ((musicValue) == 5);
  {tone(446)};
  if ((musicValue) == 6);
  {tone(523)};
  if ((musicValue) == 7);
  {tone(587)};
  if ((musicValue) == 8);
  {tone(659)};
  if ((musicValue) == 9);
  {tone(698)};
  if ((musicValue) == 0);
  {tone(784)};
  delay(20);
  noTone;
  delay(5);
  ;
}

I thought I was close with this; i got many errors. what can i do?

Moderator edit: [code] ... [/code] tags added. (Nick Gammon)

too many semicolons .... :fearful:

Go review the syntax for an if statement.

And figure out ...

// ... how to post code fragments with the code tag!

code inserts! this site has everything!
$)

now i had less ; when i started, but my error messages insisted i was missing semicolons... it lies!

any other glaring errors?

Once again:

Please edit your post, select the code, and put it between [code] ... [/code] tags.

You can do that by hitting the # button above the posting area.

would it be ideal to make it into an if else statement?

also, where are semicolons REQUIRED and when are they SYNTAX ERRORS when missed?

:.

cottewmi:
Name is Mitch, trying to advance my skills with arduino before attempting larger project (raspberry pi and arduino robot)

making a simple Light Dependant Resistor(LDR) operated synth; my intent is to simply convert the last number in the analog value of the ldr into one of 10 notes in the d minor scale. board is wired, and, to the best of my ability, my code is written, but I can see I am making plenty of mistakes in my coding; i need to learn the intricacies of arduino, and i am already past the absolute beginner stage, and want to progress into the amateur stage. please help me find the glaring errors in my programming so I may improve!

/*

Education through Hard Programming
  LDR Based SYNTHESIZER
  plays basic scales based on feedback from LDR
  Written By Mitchell Cottew
  */
#include "pitches.h"

int LDR = A5;
int out = pin3;
int LDRVal = 0;
void setup() {
// digital pin as output;
// analog pin as input;
// maybe i should do serialout too just so i know whats goin on;
pinMode(out,OUTPUT);
pinMode(LDR,INPUT);
}

void loop() {

int LDRVal = analogRead(LDR);
  int musicValue = LDRVal % 10;
  //read LDR and turn it into simple number
 
  if ((musicValue) == 0);
  {tone(294)};
  if ((musicValue) == 1);
  {tone(330)};
  if ((musicValue) == 2);
  {tone(349)};
  if ((musicValue) == 3);
  {tone(392)};
  if ((musicValue) == 4);
  {tone(440)};
  if ((musicValue) == 5);
  {tone(446)};
  if ((musicValue) == 6);
  {tone(523)};
  if ((musicValue) == 7);
  {tone(587)};
  if ((musicValue) == 8);
  {tone(659)};
  if ((musicValue) == 9);
  {tone(698)};
  if ((musicValue) == 0);
  {tone(784)};
  delay(20);
  noTone;
  delay(5);
  ;
}




I thought I was close with this; i got many errors. what can i do?

is this satisfactory? i apologize, i am unfamiliar with forum use

You only need if else if you need if else.

An if statement starts with an if and then a condition in brackets. If the condition is true or otherwise non-zero the if statement will do whatever is on the rest of the line. The semicolon marks the end of the line. Do you see what you did? (Hint: there's nothing for your if statements to do, your semicolon makes sure of that.) This error is probably causing syntax errors with the lines with braces that are now on their own.

That's not the only thing wrong, just the most glaring.

I added the code tags for you. I meant to "Modify" your original post. Anyway ...

  if ((musicValue) == 0);
  {tone(294)};

This is wrong because the first semicolon terminates the "if". Two valid ways of writing it would be:

  if (musicValue == 0)
    {
    tone(294);
    }

or:

  if (musicValue == 0)
    tone(294);

They are both quite different to what you had. A semicolon terminates a statement. However since an "if" statement is recursively defined like this:

if (condition) statement

The entire thing is one statement (an "if" statement) and thus there is one semicolon at the end (but not after the round brackets).

The braces ( "{" and "}" ) form a compound statement, and in this case you don't use a semicolon (the "}" is sufficient to terminate it).

You could use "else" in your case because musicValue is hardly going be both 1 and 2, eg.

  if (musicValue == 1)
    tone(330);
  else if (musicValue == 2)
    tone(349);
  else if (musicValue == 3)
    tone(392);
  ... and so on ...

In this sort of situation a "switch" statement would be easier to read, eg.

  switch (musicValue)
     {
     case 1 :  tone(330); break;
     case 2 :  tone(349); break;
     case 3 :  tone(392); break;
   ... and so on ...
     }  // end of switch

Never heard of a switch argument before. That's going to be VERY useful! Once I get back to the dorm I'll have to do a run check. Is there any major Problems with the setup? I'm a little confused what comes before setup and what actually constitutes the need for the setup.

It looks OK. Stuff you want to do once (if anything), you put in setup.

I don't understand this bit:

  int LDRVal = analogRead(LDR);
  int musicValue = LDRVal % 10;

Suppose that there are minor changes in the light level, and on three successive iterations of loop, LDRVal is 349, 351, and 348. The values in musicValue will be 9, 1, and 8. I don't see any good correlation between the tone played and the level of light.

I suspect that the modulo function is not what you want to be using. I suspect that you want to divide the range of the sensor output into 10 parts, and play different tones as the light level changes significantly, not as it jitters.

PaulS:
I don't understand this bit:

  int LDRVal = analogRead(LDR);

int musicValue = LDRVal % 10;



Suppose that there are minor changes in the light level, and on three successive iterations of loop, LDRVal is 349, 351, and 348. The values in musicValue will be 9, 1, and 8. I don't see any good correlation between the tone played and the level of light.

I suspect that the modulo function is not what you want to be using. I suspect that you want to divide the range of the sensor output into 10 parts, and play different tones as the light level changes significantly, not as it jitters.

My intention was not to have the analog input be used as a whole; more as a random number generator.

the last number of the value will be one of 10 options, regardless of how bright or dark it may be; it will still only be using those 10 values.

Also, its simply a different way of doing it; i haven't seen someone use the analog data like that yet, so I figured it may yield interesting patterns without being a garbled mess like some simpler opto-theremins using LDRs turn out to be. Experiment!