control LED strip with pot

greetings all,

been a long time since i have been here. was another name, but since changing it legally, so started as a new person.

currently doing a project involving addressable LED strips from here, plus code
http://coolcomponents.co.uk/catalog/strip-ledm-addressable-p-706.html

using the code given to play with it as well. but trying to control it with a 10k pot. more so the outcome will be like a light fader. ill move the pot, which then [should] control the light strip. pot goes up, lights go up. pot goes down, light go down.
but so far the lights are just changing time in which they go up, they dont come back down.
my initial plan is that the pot will be at 0 when it is half-way [0.5] that means it is standing still. then when it goes to 1. the light it go up the strip, when it goes to 0., the lights with go down the strip.

here is the code ive been playing with & will continue until i find my answer.

all the best

//LOOK AT USING THE 'VAL' FUNCTION TO PLAY WITH THE LEDS GOING UP & DOWN
//ALSOHOW TO 'DELETE' THE LIGHTS WHEN THE VALUE IS BROUGHT DOWN FROM THE POT
//  Blue = 5V / Red = SDI / Green = CKI / Black = GND

int SDI = 2; //Red wire (not the red 5V wire!)
int CKI = 3; //Green wire
int ledPin = 13; //On board LED

const int analogInPin = A0;
const int analogOutPin = 2;
int potVal = 0;
int potOut = 0;

#define STRIP_LENGTH 32 //32 LEDs on this strip
long strip_colors[STRIP_LENGTH];

void setup() {
  pinMode(SDI, OUTPUT);
  pinMode(CKI, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  //Clear out the array
  for(int x = 0 ; x < STRIP_LENGTH ; x++)
    strip_colors[x] = 0;
    
  randomSeed(analogRead(0));
  
  //Serial.begin(9600);
  //Serial.println("Hello!");
}

//----------------------------------------------------------------
void loop() {
  
  //Pre-fill the color array with known values
  strip_colors[0] = 0xFF0000; //Bright Red
  strip_colors[1] = 0x00FF00; //Bright Green
  strip_colors[2] = 0x0000FF; //Bright Blue
  post_frame(); //Push the current color frame to the strip
  
  delay(2000);

  while(1){ //Do nothing
    addRandom();
    post_frame(); //Push the current color frame to the strip

    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(potOut);                  // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(potOut);                  // wait for a second
  }
}

//----------------------------------------------------------------
//Throws random colors down the strip array
void addRandom(void) {
  
  potVal = analogRead(analogInPin);
  potOut = map(potVal, 0, 1023, 0, 100);
  analogWrite(analogOutPin, potOut);
  int x;
  
  //First, shuffle all the current colors down one spot on the strip
  for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--) strip_colors[x] = strip_colors[x - 1];
    
  //Now form a new RGB color
  long new_color = 0;
  for(x = 0 ; x < 3 ; x++){
    new_color <<= 8;
    new_color |= random(0xFF); //Give me a number from 0 to 0xFF
    //new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
  }
  
  strip_colors[0] = new_color; //Add the new random color to the strip
}

//----------------------------------------------------------------
//Takes the current strip color array and pushes it out
void post_frame (void) {
  //Each LED requires 24 bits of data
  //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0 
  //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
  //Pulling the clock low for 500us or more causes the IC to post the data.

  for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
    long this_led_color = strip_colors[LED_number]; //24 bits of color data

    for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
      //Feed color bit 23 first (red data MSB)
      
      digitalWrite(CKI, LOW); //Only change data when clock is low
      
      long mask = 1L << color_bit;
      //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
      
      if(this_led_color & mask) 
        digitalWrite(SDI, HIGH);
      else
        digitalWrite(SDI, LOW);
  
      digitalWrite(CKI, HIGH); //Data is latched when clock goes high
    }
  }

  //Pull clock low to put strip into reset/post mode
  digitalWrite(CKI, LOW);
  delayMicroseconds(500); //Wait for 500us to go into reset
}

well. after some nobbing around, ive gotten to a part where im happy taking things from.

found this very nice post http://interface.khm.de/index.php/lab-log/digital-addressable-led-strip-arduino/

this has what i need. now i just need to weed out what i need

well… ive decided to go back to the original code. mainly as the second one was too much. for something so simple, it shouldnt be that much code.
but i need help in this now. im hitting my head against imaginary walls.
so what i just need help with is the code to get the pot to control the led strip. pot goes up, the light follows. pot goes down, the light follows.
what this is supposed to create is a simple led strip that follows the value of the pot.

here is my code that i went back to & edited. but im just a bit stumped right now. i think the answer is a mixture of a ‘for’ loop & ‘if’ statements

int SDI = 2; //Red wire (not the red 5V wire!)
int CKI = 3; //Green wire
int ledPin = 13; //On board LED

const int analogInPin = A0;
const int analogOutPin = 2;
int potVal = 0;
int potOut = 0;

#define STRIP_LENGTH 32 //32 LEDs on this strip
long strip_colors[STRIP_LENGTH];

void setup() {
  pinMode(SDI, OUTPUT);
  pinMode(CKI, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  //Clear out the array
  for(int x = 0 ; x < STRIP_LENGTH ; x++)
    strip_colors[x] = 0;
    
  randomSeed(analogRead(0));
  
  //Serial.begin(9600);
  //Serial.println("Hello!");
}

void loop() {
  post_frame(); //Push the current color frame to the strip
  
  potVal = analogRead(analogInPin);
  potOut = map(potVal, 0, 1023, 0, 1000);
  analogWrite(analogOutPin, potOut);
  
  while(1){ //Do nothing
    addColor();
    post_frame(); //Push the current color frame to the strip

    digitalWrite(ledPin, HIGH);   // set the LED on
    delay(150);                  // wait for a second
    digitalWrite(ledPin, LOW);    // set the LED off
    delay(150);                  // wait for a second
  }
}

//----------------------------------------------------------------
//Throws random colors down the strip array
void addColor(void) {
  int x;
  //First, shuffle all the current colors down one spot on the strip
  for(x = (STRIP_LENGTH - 1) ; x > 0 ; x--)
    strip_colors[x] = strip_colors[x - 1];
  //Now form a new RGB color
  long new_color = 0;
  for(x = 0 ; x < 3 ; x++){
    new_color <<= 8;
    new_color = 0x000CFF; //Give me a number from 0 to 0xFF
    //new_color &= 0xFFFFF0; //Force the random number to just the upper brightness levels. It sort of works.
  }
  strip_colors[0] = new_color; //Add the new random color to the strip
}

//----------------------------------------------------------------
//Takes the current strip color array and pushes it out
void post_frame (void) {
  //Each LED requires 24 bits of data
  //MSB: R7, R6, R5..., G7, G6..., B7, B6... B0 
  //Once the 24 bits have been delivered, the IC immediately relays these bits to its neighbor
  //Pulling the clock low for 500us or more causes the IC to post the data.

  for(int LED_number = 0 ; LED_number < STRIP_LENGTH ; LED_number++) {
    long this_led_color = strip_colors[LED_number]; //24 bits of color data

    for(byte color_bit = 23 ; color_bit != 255 ; color_bit--) {
      //Feed color bit 23 first (red data MSB)
      
      digitalWrite(CKI, LOW); //Only change data when clock is low
      
      long mask = 1L << color_bit;
      //The 1'L' forces the 1 to start as a 32 bit number, otherwise it defaults to 16-bit.
      
      if(this_led_color & mask) 
        digitalWrite(SDI, HIGH);
      else
        digitalWrite(SDI, LOW);
  
      digitalWrite(CKI, HIGH); //Data is latched when clock goes high
    }
  }
  //Pull clock low to put strip into reset/post mode
  digitalWrite(CKI, LOW);
  delayMicroseconds(500); //Wait for 500us to go into reset
}

//----------------------------------------------------------------
void potControl(){
  int aa;
  potVal = analogRead(analogInPin);
  potOut = map(potVal, 0, 1023, 0, 255);
  analogWrite(analogOutPin, potOut);
}

I was going to suggest switch cases with use of the for statements. For example,

I see you mapped your pot to a value between 0 - 1023 and remapped it as a value between 0 and 100. So, set up a switchcase saying:

int potVal = analogRead(AnalogueInPin);
int potOut = map(potVal, 0, 1023, 0, 100);

if (potOut = 50){
state = 1;
}
if (potOut = <50){
state = 2;
}
if (potOut = >50){
state = 3;
}

switch (state) {
case 0:
//Initialize
break;
case 1:
//Pot = Dead Center
break;
case 2:
//Pot = All the way Left
break;
case 3:
//Pot = All the way Right
break;
}
delay(1);
}

I didn’t test this code so it may need some changes, but that’s the route I would attempt to take. You can then probably shift potOut (++ or --) in the direction you require and fill in each switch case accordingly.

groovy... ill give that a look-see. many thanks

No problem, I hope it works for you -- let me know!

I am definitely interested in your project. I considered utilizing RGB LED strips for the project I have been working on. I too am building an LED kit utilizing shift registers at the moment. I am absolutely curious to see how you've set your circuit up. Anything you care to share is appreciated!

well... luckily the led strips are 5v. so no need for anything else, other than a breadboard and uno, plus pot. im going to be doing a blog post, or just one long post when its completed