"ledring" controlled by potentiometer. Really need help with this.

Hello! So im brand new to this forum, and i have spent some time now reading some of the topics and it seems the forum consists of people with very much nowledge about this stuff! A very good place to get help with your arduino!

So i have a bit of a problem. Im currently in a dataclass, where the mainsubject is network. But a big part of my class is also c++. And to be dead honest, i havent quite got a hang of it yet! So we've got an assignment for this wednesday, and im really struggeling to even just get startet.

This is the assignenment (we're supposed to use the arduino of course, and we're using the UNO model with the sparkfun inventors kit):

8 leds that are hooked up in a circle, and its supposed to light up 1 at a time as it passes the hole circle, and loop. Than the speed of this this "sequense" is supposed to be adjusted by an potentiometer. in other words, you are supposed to set the speed of the "ledring" with and pot.meter.

so i have to make a sequense that make a loop where the leds lit up 1 by one untill its passes the ring, then loops again. And i have to find a way to adjust the speed of the "Light" with an potmeter.

Could someone out there write an fullversion of this program for me too try, that works? so i can get a full overview of the program! i bet this is not a big job for the more novice arduino users out here, but it would be very helpful for me !

again, great forum. :smiley: :smiley:

My english is pretty bad, so just ask if there something you dont understand.

in advance; thanks!

Could someone out there write an fullversion of this program for me too try, that works?

It is your assignment, are you asking some one to do it for you?
If so we are not keen on that sort of thing here. We will help you but not do it for you. What is the point in cheating?

Break the project into pieces. You need to be able to turn 1 LED on and off.
You need to be able to turn 8 LEDs on and off, one at a time.
You need to be able to pause for some period of time between turning the nth LED off and the n+1st one on, and when you turn the n+1st LED off and the n+2nd LED off. Use a variable to hold the delay time.
You need to be able to pause for a period of time that is based on the potentiometer reading, so, you need to be able to connect the potentiometer correctly, and you need to be able to read a value from it.
Finally, you need to put it all together.

This really shouldn't take more than 10 minutes to get the first LED going on and off. Another half hour to loop around an array of pins. Twenty more minutes to wire up the potentiometer and make the delay value a function of the reading. An hour tops to get the whole thing written.

No, it is not my intention too cheat in anyway. But im really trying to understand this, and im no getting much help at school. we are 30+ class, and the teacher doesnt seem interrested to help at all. think he would find it best, if we just do what we were told and shut up.

so yes maybe its a bit much to ask for the whole program, but could you get me started? or at least show me the part where the values from the pot.meter adjust the speed/deacreses the delay?

or at least show me the part where the values from the pot.meter adjust the speed/deacreses the delay?

Sure. You show us how the potentiometer is connected to the Arduino (which pin do we need to read from?), and we'll help.

The function that you need is analogRead(). It returns a value between 0 and 1023, depending on how the pot is set. You could just use the value returned in a delay() call.

int delayTime = analogRead(potPin);
for(byte i=0; i<8; i++)
{
   // Turn pin i off. 
   // Turn pin i+1 (or 0) on

   delay(delayTime);
}

All this goes in loop(), of course.

The internet is an abundant resource. Try searching with keywords: arduino, leds, sequence. I did and one hit brought me back to this site with a tutorial for a For Loop. The hardware in the example circuit uses leds. - Scotty

PaulS:

or at least show me the part where the values from the pot.meter adjust the speed/deacreses the delay?

Sure. You show us how the potentiometer is connected to the Arduino (which pin do we need to read from?), and we'll help.

The function that you need is analogRead(). It returns a value between 0 and 1023, depending on how the pot is set. You could just use the value returned in a delay() call.

int delayTime = analogRead(potPin);

for(byte i=0; i<8; i++)
{
   // Turn pin i off.
   // Turn pin i+1 (or 0) on

delay(delayTime);
}



All this goes in loop(), of course.

thanks for answer.

So if i have understand it correctly, the for function will loop it as long as the programs running? and it will loop thru all pins, on at a time with the delay that i decide?

I havent really hooked anything up yet , and we're not allowed to bring the kit home. but we use an own made 3pin standard pot.meter. nothing fancy. So its kinda the same which pin i use, but should i use one of the analog pins? and how should i hook up the pot.meter?

So if i have understand it correctly, the for function will loop it as long as the programs running? and it will loop thru all pins, on at a time with the delay that i decide?

Not quite. loop() will loop forever. within that you will put in a for loop.

The example given will loop 8 times. The first time through, i will contain 0, then i will contain 1 higher each time though the for, so it will contain 0, 1, 2, 3, 4, 5, 6, 7 for the 8 times through the for loop.

You will want to change the starting and terminating values to suit the pins you are actually using.

You will want to change the starting and terminating values to suit the pins you are actually using.

Even better would be to use an array for the pin number you actually use. i would be the index into the array, inside the for loop.

Okey, thanks for answers.

I have spent most of my evening reading up on which function to choose. im com up with this so far

int sensorValue = 0;    //too store incoming
                        //analog values

void setup(){
  pinMode(12, OUTPUT);  //
  pinMode(11, OUTPUT);  // for (output).
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);

  Serial.begin(9600);  //initialize serial
}

void loop(){      //code executed in a loop 

Serial.print("sensor = " );  //sends what's in quotes via serial
Serial.println(sensorValue); //sends the variable (sensorValue)
                             //via serial   

  digitalWrite(12,HIGH);       // lights the led
  sensorValue = analogRead(0); // reads pin 0
  delay(sensorValue + 25);     // sensorValue used for delay
  digitalWrite(12,LOW);        //turns off the led
  delay(15);                   //delay before moving to next output pin
                               //the + 25 keeps delay from reaching zero

how does this look? i have only written the output code for one of the leds, but i can just use the same code on them? with an different outputPin ofcourse.

void loop(){      //code executed in a loop 

Serial.print("sensor = " );  //sends what's in quotes via serial
Serial.println(sensorValue); //sends the variable (sensorValue)
                             //via serial

You don't have anything to print, yet. Why are you printing right off the bat?

  sensorValue = analogRead(0); // reads pin 0
  delay(sensorValue + 25);     // sensorValue used for delay

Now, you have something to print and use. Why aren't you printing it now?

void loop(){      //code executed in a loop

Break this habit RIGHT NOW. The { goes on a new line. ALWAYS!

The loop() function is missing a closing }.

i have only written the output code for one of the leds, but i can just use the same code on them? with an different outputPin ofcourse.

I hope that you don't mean that you are going to copy the same code another 7 times and change the pin numbers.

Put the pin numbers in an array then increment a variable each time through loop() to provide an index to the array to get the pin number for the current LED. Once the index variable reaches 8 set it back to zero.

Once you get that working you may find the delay() becoming a nuisance because everything stops whilst the delay() occurs. You might like to investigate the principle used in the BlinkWithoutDelay example in the IDE so that the increment of the index variable happens at a frequency determined by the sensor input.

but, do i not need the

Serial.print("sensor = " );
Serial.println(sensorValue);

to get the variabels via serial? or the arduino wont understand what to the with the analog values from the pot.meter?

do i need to do it this advanced? i feel like going for my solution.

but, do i not need the
Code:

Serial.print("sensor = " );
Serial.println(sensorValue);

to get the variabels via serial?

You need those statements to see the values in the Serial Monitor application. But, what value are you printing? Not the one read from the sensor, because you have the print statements BEFORE you read from the sensor.

There is no reason for sensorValue to be global, since it is used only in one function.

aaah okay. Im printing the sensorValue aka the analogRead pin 0 values. So i just have to move my print statments to after the sensorValue = analogRead(0) codeline?

Or i write it in the buttom of the hole output code?

So i just have to move my print statments to after the sensorValue = analogRead(0) codeline?

Yes.

Here is what I came up with but the speed of leds is not updating as you rotate the potentiomenter but instead is doing its loop then reinitiates a new loop with the new speed.

const byte leds = 8; // how many leds
byte pins[] = {5, 6, 7, 8, 9, 10, 11, 12}; // pins to connect leds
int pot = A1;
int minSpeed = 500;
int maxSpeed = 50;

void setup()
{

	for(int i = 0; i < leds; i++) {
		pinMode(pins[i], OUTPUT);
	}  
	pinMode(pot, INPUT);
	Serial.begin(9600);
}

void loop()
{
	int valPot = analogRead(pot);
	int speed = map(valPot, 0, 1023, minSpeed, maxSpeed);
    Serial.println(speed);
	for(int i = 0; i < leds; i++) {
		digitalWrite(pins[i], HIGH);
		delay(speed);
		digitalWrite(pins[i], LOW);
	}	
}

but the speed of leds is not updating as you rotate the potentiomenter but instead is doing its loop then reinitiates a new loop with the new speed.

So move the pot read inside the loop.

It works!! With correct readings in the seriall app.

here's my final code:

int sensorValue = 0;    //make a variable where you can store incoming analog values

void setup(){
  pinMode(2, OUTPUT);  //tell arduino what you'll be using these pins for (output)
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);
  
  Serial.begin(9600);  //initialize serial
}

void loop()
{



  digitalWrite(2,HIGH);       // lights the led
  sensorValue = analogRead(0);
  Serial.print("sensor = " );  //sends what's in quotes via serial
  Serial.println(sensorValue); // reads pin 0
  delay(sensorValue + 25);     // sensorValue used for delay
  digitalWrite(2,LOW);        //turns off the led
  delay(15);                   //delay before moving to next output pin
                               //the + 25 keeps delay from reaching zero
 

  digitalWrite(3,HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " );
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(3,LOW);
  delay(15);

  digitalWrite(4,HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " );
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(4,LOW);
  delay(15);

  digitalWrite(5,HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " )
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(5,LOW);
  delay(15);

  digitalWrite(6, HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " );
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(6, LOW);
  delay(15);

  digitalWrite(7,HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " );
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(7,LOW);
  delay(15);

  digitalWrite(8,HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " ); 
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(8,LOW);
  delay(15);

  digitalWrite(9,HIGH);
  sensorValue = analogRead(0);
  Serial.print("sensor = " );
  Serial.println(sensorValue);
  delay(sensorValue + 25);
  digitalWrite(9,LOW);
  delay(15);

}

Groove:
So move the pot read inside the loop.

Good idea, I would have never thought about it :slight_smile:
So the new code inside the loop() is:

void loop()
{
	for(int i = 0; i < leds; i++) {
		digitalWrite(pins[i], HIGH);
		int valPot = analogRead(pot);
		int speed = map(valPot, 0, 1023, minSpeed, maxSpeed);
		delay(speed);
		digitalWrite(pins[i], LOW);
	}	
}

See the video presentation of the working project http://www.youtube.com/watch?v=K1gtyabAUoo