Go Down

Topic: Need someones help shorten the code i wrote. (Read 978 times) previous topic - next topic

pdkkid

hi, im new to programming with the arduino and i wrote this code to do a simple knight rider led circuit with 2 shift registers and a led in between them, and i had to write out all the coding for it. I was wondering if there was any way to shorten the code i wrote, if you do know how to shorten it, could you also describe how you did it. thanks :D

Code: [Select]
#define data 2
#define clock 3
#define data1 4
#define clock1 5

int time = 5;

byte a = B10000000;
byte b = B01000000;
byte c = B00100000;
byte d = B00010000;
byte e = B00001000;
byte f = B00000100;
byte g = B00000010;
byte h = B00000001;
byte i = B00000000;
byte j = B00000010;
byte k = B00000100;
byte l = B00001000;
byte m = B00010000;
byte n = B00100000;
byte o = B01000000;
byte p = B10000000;
byte q = B00000000;

void setup()
{
  pinMode(clock, OUTPUT); // make the clock pin an output
  pinMode(data , OUTPUT); // make the data pin an output3
  pinMode(data1, OUTPUT);
  pinMode(clock1, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop()
{
  shift(a);
  shift(b);
  shift(c);
  shift(d);
  shift(e);
  shift(f);
  shift(g);
  shift(h);
  shift(i);
  flash(time);
  shift1(a);
  shift1(b);
  shift1(c);
  shift1(d);
  shift1(e);
  shift1(f);
  shift1(g);
  shift1(h);
  shift1(j);
  shift1(k);
  shift1(l);
  shift1(m);
  shift1(n);
  shift1(o);
  shift1(p);
  shift1(q);
  flash(time);
  shift(h);
  shift(j);
  shift(k);
  shift(l);
  shift(m);
  shift(n);
  shift(o);
}

void shift(int letter)
{
  shiftOut(data, clock, LSBFIRST, letter);
  delay (time);
}

void shift1(int letter)
{
  shiftOut(data1, clock1, LSBFIRST, letter);
  delay(time);
}

void flash(int duration)
{
  digitalWrite(7, HIGH);
  delay(duration);
  digitalWrite(7, LOW);
}

Graynomad

#1
May 26, 2012, 05:53 am Last Edit: May 26, 2012, 07:58 am by Graynomad Reason: 1
This won't reproduce your bit pattern exactly and I haven't got time to analyse why you have two clock pins, but it should give you an idea.

Code: [Select]
#define data 2
#define clock 3
#define data1 4
#define clock1 5

int time = 5;

void setup()
{
  pinMode(clock, OUTPUT); // make the clock pin an output
  pinMode(data , OUTPUT); // make the data pin an output3
  pinMode(data1, OUTPUT);
  pinMode(clock1, OUTPUT);
  pinMode(7, OUTPUT);
}

void loop()
{
   shift_to_the_right (clock);
   flash(time);
   shift_to_the_right (clock1);
   shift_to_the_left (clock1);
   flash(time);
   shift_to_the_left (clock);
}

void shift_to_the_right (int clockPin) {

   int x = B10000000;
for (int i = 0; i < 8; i++) {
    shift (x, clockPin);
    x >>= 1;  // moved bit to next right position
}
}

void shift_to_the_left (int clockPin) {

   int x = B00000001;
for (int i = 0; i < 8; i++) {
    shift (x, clockPin);
    x <<= 1;  // moved bit to next left position
}
}

void shift(int pattern, int clockPin)
{
  shiftOut(data, clockPin, LSBFIRST, pattern);
  delay (time);
}

void flash(int duration)
{
  digitalWrite(7, HIGH);
  delay(duration);
  digitalWrite(7, LOW);
}



______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

pdkkid

#2
May 26, 2012, 08:09 pm Last Edit: May 26, 2012, 08:12 pm by pdkkid Reason: 1
thank you very much for your help with this but i was wondering how this line of code works,
Code: [Select]
for (int i = 0; i < 8; i++)
ive seen this line of code in alot of different projects but i don't know how it works, if you could explain or point me to a website of video that explains this, it would help alot.

also i have 2 data and clocks because i used 2 different 74HC164 shift registers to control the 16 leds that i have setup

thanks,
pdkkid

WizenedEE

http://lmgtfy.com/?q=for+C

It just counts to 7

RPCoyle

in plain English, what it says is "for variable i (which is an integer) start by making it equal to 0, then increment it by 1 until it gets to one integer less than 8 then stop doing whatever it is supposed to do.

Ain't C++ wonderful !!!

pdkkid


in plain English, what it says is "for variable i (which is an integer) start by making it equal to 0, then increment it by 1 until it gets to one integer less than 8 then stop doing whatever it is supposed to do.

Ain't C++ wonderful !!!


thank you very much, if you could tho help with with the rest of this code where i found it
Code: [Select]
void shift_to_the_right (int clockPin) {

   int x = B10000000;
for (int i = 0; i < 8; i++) {
    shift (x, clockPin);
    x >>= 1;  // moved bit to next right position
}
}


again im wondering how this code outputs 1st B10000000 then it does B01000000. How does it move the 1 through it?.

Grumpy_Mike

Quote
How does it move the 1 through it?.

The
Code: [Select]
x >>= 1;  // moved bit to next right position
operation uses the >> shift to the right , hint read the comment.

robtillaart


The refence section of arduino.cc explains quite a lot of comon programming constructs.

furthermore a good book is about programming is - http://zanasi.chem.unisa.it/download/C.pdf - a must buy book BTW
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

lloyddean

#8
May 27, 2012, 09:34 pm Last Edit: May 27, 2012, 11:51 pm by lloyddean Reason: 1
Lookup table driven version.

Shorter code using data lookup table which could be placed in PROGMEM.

Code: [Select]

/* =====================================================================================================================
* File - KnightRider.pde
* ---------------------------------------------------------------------------------------------------------------------
* <http://arduino.cc/forum/index.php/topic,107403.0.html>
*/

const uint8_t           pinDATA0    = 2;
const uint8_t           pinCLOCK0   = 3;
const uint8_t           pinDATA1    = 4;
const uint8_t           pinCLOCK1   = 5;

const uint8_t           pinFLASH    = 7;

const uint8_t           FLASH_OFF   = LOW;
const uint8_t           FLASH_ON    = HIGH;

const unsigned long     dmsLIT      = 5UL;      // delta, in milliseconds, LIGHT ON

const uint8_t           lookup[] =
{
      0b10000000    //  0
    , 0b01000000    //  1
    , 0b00100000    //  2
    , 0b00010000    //  3
    , 0b00001000    //  4
    , 0b00000100    //  5
    , 0b00000010    //  6
    , 0b00000001    //  7

    , 0b00000000    //  8       flash and change register set
   
    , 0b10000000    //  9
    , 0b01000000    // 10
    , 0b00100000    // 11
    , 0b00010000    // 12
    , 0b00001000    // 13
    , 0b00000100    // 14
    , 0b00000010    // 15
    , 0b00000001    // 16
    , 0b00000010    // 17
    , 0b00000100    // 18
    , 0b00001000    // 19
    , 0b00010000    // 20
    , 0b00100000    // 21
    , 0b01000000    // 22
    , 0b10000000    // 23

    , 0b00000000    // 24       flash and change register set
   
    , 0b00000001    // 25
    , 0b00000010    // 26
    , 0b00000100    // 27
    , 0b00001000    // 28
    , 0b00010000    // 29
    , 0b00100000    // 30
    , 0b01000000    // 31
};

void flash(int dms)
{
    digitalWrite(pinFLASH, FLASH_ON);
    delay(dms);
    digitalWrite(pinFLASH, FLASH_OFF);
}

void shift(uint8_t const data, uint8_t const rindex)
{
    // shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, byte value)

    const uint8_t   pinDATA[]   = { pinDATA0 , pinDATA1  };
    const uint8_t   pinCLOCK[]  = { pinCLOCK0, pinCLOCK1 };

    shiftOut(pinDATA[rindex], pinCLOCK[rindex], LSBFIRST, data);
    delay(dmsLIT);
   
    if ( 0 == data )
    {
        flash(dmsLIT);
    }
}

void loop()
{
    uint8_t rindex = 0;
   
    for ( size_t i = 0; i < sizeof(lookup); i++ )
    {
        uint8_t     data = lookup[i];
        shift(data, rindex);

        if ( 0 == data )
        {
            rindex = ((++rindex) & 1);
        }
    }
}

void setup()
{
    pinMode(pinCLOCK0, OUTPUT);
    pinMode(pinDATA0 , OUTPUT);
    pinMode(pinCLOCK1, OUTPUT);
    pinMode(pinDATA1, OUTPUT);
    pinMode(pinFLASH, OUTPUT);
}

Graynomad

Quote
a must buy book BTW

Ah the "KnR" book, an oldy but a goody. My Bible a long time ago.

______
Rob

Rob Gray aka the GRAYnomad www.robgray.com

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy