Pages: [1]   Go Down
Author Topic: Need someones help shorten the code i wrote.  (Read 821 times)
0 Members and 1 Guest are viewing this topic.
Temecula, California
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Im Learning..... Give me a Break
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 smiley-grin

Code:
#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);
}
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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
« Last Edit: May 26, 2012, 12:58:17 am by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Temecula, California
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Im Learning..... Give me a Break
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thank you very much for your help with this but i was wondering how this line of code works,
Code:
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
« Last Edit: May 26, 2012, 01:12:39 pm by pdkkid » Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

It just counts to 7
Logged

Santa Fe
Offline Offline
Full Member
***
Karma: 1
Posts: 201
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 !!!
Logged

Temecula, California
Offline Offline
Newbie
*
Karma: 0
Posts: 3
Im Learning..... Give me a Break
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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?.
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 602
Posts: 33373
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
How does it move the 1 through it?.
The
Code:
x >>= 1;  // moved bit to next right position
operation uses the >> shift to the right , hint read the comment.
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 212
Posts: 13531
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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
Logged

Rob Tillaart

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

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Lookup table driven version.

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

Code:
/* =====================================================================================================================
 * 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);
}
« Last Edit: May 27, 2012, 04:51:51 pm by lloyddean » Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 126
Posts: 8472
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

______
Rob

Logged

Rob Gray aka the GRAYnomad www.robgray.com

Pages: [1]   Go Up
Jump to: