Pages: [1]   Go Down
Author Topic: controlling the speed/rate with a shift register outs(kinda Larson scanner)  (Read 1041 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 0
Posts: 353
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Picking your brains and experience so i can apply to something i need.
I need to control a VCO using a shift register( lets say a 595).
On the outputs i can control the voltage with a resistive ladder(R2R),DAC style, so in that sense no doubts.
My doubt is this: Would the code below be the best way to have a control of its speed/rate ?! Is there a better, with better response way to do that ?! If so, can you lead me onto the right path, please ?!
Im developing a sound effects box, and despite already having achieved what id set myself to, id love to add more extras, and having a better control over it would be just brilliant to what i need .


Code:
 //Pin connected to ST_CP of stop
int latchPin = 8;
//Pin connected to SH_CP of stop
int clockPin = 12;
//Pin connected to DS of stop
int dataPin = 11;
int delayInput = 0;
int outDelay =0;

// Out to 74HC595 (Serial TO Parallel OUT)

void stopout(int numberToDisplay)
{
  // take the latchPin low so
  // the LEDs don't change while you're sending in bits:
  // digitalWrite(latchPin, LOW);
  // shift out the bits:
  shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);

  //take the latch pin high so the LEDs will light up:
  digitalWrite(latchPin, LOW);
  digitalWrite(latchPin, HIGH);
}

void setup() {
  //set pins to output so you can control the shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(delayInput, INPUT);
  //Lets Clear the LEDs - just incase the 74HC595 'remembers' its last state after restart.
  stopout(B00000000);
  delay(500);
}

void loop() {
  outDelay = analogRead (delayInput);
  stopout(B10000000);
  delay(outDelay);
  stopout(B01000000);
  delay(outDelay);
  stopout(B00100000);
  delay(outDelay);
  stopout(B00010000);
  delay(outDelay);
  stopout(B00001000);
  delay(outDelay);
  stopout(B00000100);
  delay(outDelay);
  stopout(B00000010);
  delay(outDelay);
  stopout(B00000001);
  delay(outDelay);
  stopout(B00000010);
  delay(outDelay);
  stopout(B00000100);
  delay(outDelay);
  stopout(B00001000);
  delay(outDelay);
  stopout(B00010000);
  delay(outDelay);
  stopout(B00100000);
  delay(outDelay);
  stopout(B01000000);
}

 
Logged

10 LET Loop=Infinite
20 GO TO 10

Offline Offline
Sr. Member
****
Karma: 0
Posts: 353
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Njay was kind enough to note a nice way of doing it !
Code:
/*este e um caso basico para que possa perceber facilmente qualquer implementacao.
*Pino conectado ao ST_CP do stop*/
int latchPin = 8;
//Pino conectado ao SH_CP of stop
int clockPin = 12;
//Pino conectado ao DS do stop
int dataPin = 11;
int delayInput = 0;//pin para entrada analogica, neste caso um potenciometro
int outDelay =0;//O valor que vamos usar para converter  a leitura do pin analogico
                //do pot para usar como valor de delay entre accoes

// Saida para 74HC595 (Serial TO Parallel OUT)

void stopout(int numberToDisplay)
{
  // baixar o latchPin para low para que
  // os LEDs nao mudem enquanto os bits sao enviados:
  // digitalWrite(latchPin, LOW);
  // passar os bits:
  shiftOut(dataPin, clockPin, MSBFIRST, numberToDisplay);

  //mudar o latch pin para HIGH para que os leds acendam:
  digitalWrite(latchPin, LOW);
  digitalWrite(latchPin, HIGH);
}

void setup() {
  //Configurar os pins para saida, para poder controlar o shift register
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(delayInput, INPUT);//Atribuir o pin a entrada analogicado potenciometro
  //Apagar qualquer memoria que o 74HC595 possa ter da ultima configuracao dos pins
  stopout(B00000000);
  delay(500);
}


void loop ()
{
    outDelay = analogRead (delayInput);
    for (unsigned char ctr = 0, mask = 0x80; ctr < 14; ctr++)
    {
        stopout(mask);
        delay(outDelay);
        mask = (ctr < 7? mask >> 1: mask << 1);
    }
}
Logged

10 LET Loop=Infinite
20 GO TO 10

Philadelphia PA
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello all,

I love the simplicity of this code.  However being a noob I have to ask:

Code:
mask = (ctr < 7? mask >> 1: mask << 1);

can some one break this down for me?

Is it a short code for if...then...else?

thank you
Logged

Valencia, Spain
Online Online
Faraday Member
**
Karma: 150
Posts: 5660
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Is it a short code for if...then...else?

Yes, but it's not something you should try to emulate/use...

(it causes code readability problems like the one you're experiencing right now)
Logged

No, I don't answer questions sent in private messages (but I do accept thank-you notes...)

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Is it a short code for if...then...else?

Yes. It controls the direction of flow for the leds based on the value of ctr.
Logged

Pages: [1]   Go Up
Jump to: