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)
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
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.
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.
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.
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!