void loop()!

Hello! Can anyone help me with this part of my code?

digitalWrite(speakerPin7, HIGH);
digitalWrite(speakerPin8, LOW);
delayMicroseconds(val/(val4));
digitalWrite(speakerPin7, LOW);
digitalWrite(speakerPin8, HIGH);
delayMicroseconds(val/(val4));

The code works fine for what I’m doing but I wish to incorporate an LED that will blink according to a different value from another pot! The problem is I end up with different delays all lumped together and of course the code won’t run properly. How do I separate these actions? Thanks, Goatboy

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1237741475

/\ /\ /\
| | |
this code does a great job of scheduling events. if you want something to occur some fixed time after another event you can use something like

if (millis<=t+500)
   {
     DoSomething;
   }
   else
   {
     DoSomeOtherThing;
   }

you can put t=millis in the scheduled part of the code and then have the if/then run in the code in the bottom so it will constantly refresh. there are better ways, but this is off the top of my head.

Thanks 80HD!! I’m afraid this is still rocket science to me and not sure what I have to do with what you have shown me :o Could you possibly give me an example using the code I have? I have included digitalWrite ledPin HIGH & LOW. I just need to have them blink according to (val5) for example. Really appreciate the input thanks, Goatboy

void loop() {
digitalWrite(speakerPin7, LOW);
digitalWrite(speakerPin8, HIGH);

val4 =analogRead(4)/4; // read pot 4, delay adjust
val3 =analogRead(3)/32; // read pot 3, fine tune low cycle rate
val2 =analogRead(2)/val3; // read pot 2, mess with the cycles
val1 =analogRead(1) /4; // read pot 1, mess with pitch
val =analogRead(0); // read value from the LDR
val =val*val1; // process the value a little
//val = val/2; // process the value a little

for( int i=0; i<val2; i++ ) { // cycles
digitalWrite(speakerPin7, HIGH);
digitalWrite(speakerPin8, LOW);
digitalWrite(ledPin, HIGH);
delayMicroseconds(val/(val4)); // try and attach pot 2controll this
digitalWrite(speakerPin7, LOW);
digitalWrite(speakerPin8, HIGH);
digitalWrite(ledPin, LOW);
delayMicroseconds(val/(val4)); // try and attach pot 2controll this
}
}
:)I’ve been checking my “GettingStartedWithArduino” and see what I think are various possibilities. “if” , “Switch case” , “do” and also “break”! Am I on the right track track anyone? :-/ Can I put several “if” statements one after the other where one relates to digitalWrite for speakerPin and another relates to digitalWrite for ledPin? Thanks anyway, Goatboy :slight_smile:

I would like to see all your code.

Either use the Copy For Forum command in the arduino IDE, or use the '#' in the forum reply editor thingy :)

I'll gladly help.

int potPin = 0; // select the input pin for the LDR

int speakerPin7 = 7; // Left Speaker
int speakerPin8 = 8; // Right Speaker

int val = 0; // analog input 0
int val1 = 0; // analog input 1
int val2 = 0; // analog input 2
int val3 = 0; // analog input 3
int val4 = 0; // analog input 4

void setup() {
pinMode(speakerPin7, OUTPUT); //Left Speaker
pinMode(speakerPin8, OUTPUT); //Right Speaker
Serial.begin(9600);

}

void loop() {
digitalWrite(speakerPin7, LOW);
digitalWrite(speakerPin8, HIGH);

val4 =analogRead(4)/4; // read pot 4, delay adjust
val3 =analogRead(3)/32; // read pot 3, fine tune low cycle rate
val2 =analogRead(2)/val3; // read pot 2, mess with the cycles
val1 =analogRead(1) /4; // read pot 1, mess with pitch
val =analogRead(0); // read value from the LDR
val =val*val1; // process the value a little

for( int i=0; i<val2; i++ ) { // cycles
digitalWrite(speakerPin7, HIGH); // Left Speaker
digitalWrite(speakerPin8, LOW); // Right Speaker
delayMicroseconds(val/(val4));
digitalWrite(speakerPin7, LOW);
digitalWrite(speakerPin8, HIGH);
delayMicroseconds(val/(val4));
}
}

Originally the code started out as TodBot’s Theremin sketch but after messing with it I wanted to manipulate " val " in the " for " part using a pot and then I ended up with a load of pots getting some great sounds. What i would love to do is add an Led that Blinks according to " val " but my lack of programming skills leaves me miffed on how to do it :-/! Really appreciate the help AlphaBeta! Goatboy :slight_smile: