Pages: [1]   Go Down
Author Topic: rewrite function from struct  (Read 414 times)
0 Members and 1 Guest are viewing this topic.
Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,
I'm building a clock that uses the LPD6803 lib from ladyada. Since the clock crashes a lot I want to "port" the clock to this fastspi lib. Trust me when I say I'm a novice programmer... I managed to change most of the code but I don't understand how to rewrite the struct (?). h=actual hour (led nr), 0,0,brightness is RGB values.
Code:
strip.setPixelColor(h, 0, 0, brightness);

should become something like this: (0=led nr and should become h, m and s variable)
Code:
leds[0].r = 0;
  leds[0].b = 0;
  leds[0].g = 0;

Th is the entire code. Most is done but the above part I don't understand.
Code:
// LPD6803 SPI pins
//dataPin = 11;       // 'white' wire
//clockPin = 13;      // 'green' wire

//DS1307 pins
//SDA = A4
//SCL = A5
//SQW = 2

//include needed libraries
#include <FastSPI_LED.h>
#include <Wire.h>
#include "RTClib.h"

// RTC_Millis is for a soft rtc
RTC_DS1307 RTC;

#define NUM_LEDS 60
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB *leds;

// LDR pin
int LDRPin = A2;
// LDR value
int LDRValue;
// Brightness fade up (1 or 2) depens on LDR reading
int z;
// Set brightness min 6, max 31.
int MinBright = 6;
int MaxBright = 31;
int brightness;

// Define second, minute, hour
int s;
int m;
int h;

void setup()
{
  Serial.begin(19200);
  Wire.begin();
  RTC.begin();
              {
    // following line sets the RTC to the date & time this sketch was compiled
    RTC.adjust(DateTime(__DATE__, __TIME__));
               }
  // set 1hz sqw on DS1307 pin 7, we will using this for a interrupt on arduino pin 2
  Wire.beginTransmission(0x68);              // write the control register
  Wire.send(0x07);                           // register address 07H)
  Wire.send(0x90);                           // 0x90=1Hz, 0x91=4kHz, 0x92=8kHz, 0x93=32kHz
  Wire.endTransmission();
  
  //Attach pin 7 from DS1307 to Arduino pin 2 and call function clock
  attachInterrupt(0, clock, FALLING);
  
  FastSPI_LED.setLeds(NUM_LEDS);
  FastSPI_LED.setChipset(CFastSPI_LED::SPI_LPD6803);
  FastSPI_LED.setDataRate(2); //set pixel data rate
  FastSPI_LED.init();
  FastSPI_LED.start();

  leds = (struct CRGB*)FastSPI_LED.getRGBData();
} //end setup


// Empty loop, all is done by the 1hz interupt on pin 2
void loop () { }

void clock() {
    interrupts(); //start all interupts again.
    
  //attach Interrupt stops the strip, so start it again  
  //strip.begin();
  FastSPI_LED.show();

  //Get current time
  DateTime now = RTC.now();  
  
  // mapping hour 24 => 12 => 60 (hour hand)
  m = now.minute(), DEC;
  s = now.second(), DEC;
  h = now.hour(), DEC;
  if (h >= 12) { h = h - 12; }
  else if (h = 12) { h = 0; }
  h = map(h, 0, 12, 0, 60);
  
  if ( m <= 6 ) { h == h; }
  else if ( m <= 18 ) { h = h + 1; }
  else if ( m <= 30 ) { h = h + 2; }
  else if ( m <= 42 ) { h = h + 3; }
  else if ( m <= 54 ) { h = h + 4; }
  
    // Get LDR vaulue and set brightness  
    LDRValue = analogRead(LDRPin);
    brightness = map(LDRValue, 0, 1023, MaxBright, MinBright);
  
    // set increase step
    if ( brightness < 20 ) {
      z = 1;
    } else {
      z = 2;
    }
    
    strip.setPixelColor((m - 1), 0, 0, 0);
    strip.setPixelColor((s - 2 ), 0, 0, 0);
    strip.setPixelColor((s - 3 ), 0, 0, 0);
    
    
    // clear transistion from 59 -> 0
    if (s == 0) { strip.setPixelColor((58), 0, 0, 0); }
    if (s == 1) { strip.setPixelColor((59), 0, 0, 0); }  
    if (m == 0) { strip.setPixelColor((59), 0, 0, 0); }
    
    //start fade up/down
    for (int y = 1; y < brightness; y = y + z) {
  
         strip.setPixelColor((s - 1 ), (brightness - y), 0, 0);
         if (s == 0) { strip.setPixelColor(59, (brightness - y), 0, 0); }  
         strip.setPixelColor(s, brightness, 0, 0);
         strip.setPixelColor((s + 1 ), y, 0, 0);
         if (s == 59) { strip.setPixelColor(0, y, 0, 0); }
        
         strip.setPixelColor(m, 0, brightness, 0);  
         strip.setPixelColor(h, 0, 0, brightness);
        
         // second equals minute
         if ((s + 1) == m) { strip.setPixelColor(s + 1, y, brightness, 0); }
         if (s == m) { strip.setPixelColor(s, brightness, brightness, 0); }
         if ((s - 1) == m) { strip.setPixelColor(s - 1, (brightness - y), brightness, 0); }
        
         // second equals hour
         if ((s + 1) == h) { strip.setPixelColor(s + 1, y, 0, brightness); }
         if (s == h) { strip.setPixelColor(s, brightness, 0, brightness); }
         if ((s - 1) == h) { strip.setPixelColor(s - 1, (brightness - y), 0, brightness); }
    
         // update strip
         //strip.show();
         FastSPI_LED.show();
        
         delay(105 - (2 * y));
     }
   // Debug  
    //Serial.print(now.year(), DEC);
    //Serial.print('/');
    //Serial.print(now.month(), DEC);
    //Serial.print('/');
    //Serial.print(now.day(), DEC);
    //Serial.print(' ');
    //Serial.print(now.hour(), DEC);
    //Serial.print(':');
    //Serial.print(now.minute(), DEC);
    //Serial.print(':');
    //Serial.print(now.second(), DEC);
    //Serial.println();
    Serial.print("raw LDR reading = ");
    Serial.println(LDRValue);     // the raw analog reading
    Serial.print("mapped brightness level = ");
    Serial.println(LDRPin);     // the analog level
    Serial.print("brightness level = ");
    Serial.println(brightness);     // the calculated level
    Serial.println();
}
« Last Edit: January 31, 2012, 03:18:18 pm by RogerMoerdijk » Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 178
Posts: 8060
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You probably want an array of structures:
Code:
#define NUM_LEDS 60
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
struct CRGB leds[NUM_LEDS];

That can be shortened to:
Code:
#define NUM_LEDS 60
struct CRGB { unsigned char r; unsigned char g; unsigned char b; } eds[NUM_LEDS];
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi John,
thx for yor reply.

what I need is something in this style:    strip.setPixelColor(NumLed, R, G, B);
this piece of code is taken from the original 6803 lib from adafruit.

what you call the struct should take four arguments instead of three.

"leds[0].r = 0;
  leds[0].b = 0;
  leds[0].g = 0;"  takes three lines and beside, I don't know how to transform this into one line (taking four arguments. And how to change the
  • into a variable (like hours... smiley-wink

Maybe you didn't notice, but I'm a newbie... smiley-wink
I'm a bit dislectyc so it's quit a pain for me stairing at the lib's and write the transform.
Logged

South Texas
Offline Offline
Edison Member
*
Karma: 8
Posts: 1023
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A struct is a way of grouping several variables together under a common name. The variables in the struct do not have to be the same type.

A common example (outside of the Arduino IDE) was  an address record or struct(ure) you hade name, address, zip, soc.sec.no, and other realted data all stuck in 1 struct because of their relationship.

so - you have created a struct with values for the 3 colors at that point -
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
this is desctibing a record with the values for R, G ,& B

You then describe another struct using the same name (probably adds to the confusion) that is an array of the previous struct

I would have thought you would have done this -
struct CRGB { unsigned char r; unsigned char g; unsigned char b; };
 and use it as a new type , and then define an array -
CRGB leds[60];

and you would address them as leds[1].r, leds[1].g and leds[1].b

You are calling a function -     
strip.setPixelColor((m - 1), 0, 0, 0);
which probably has something like this -
strip.setpixel (int index, unsigned char red; unsigned char green; unsigned char blue)
 and has code something like this -
   leds[index].r=red;
   leds[index].g=green;
   leds[index].b=blue;
...
but this allows you to work with a "variable"  called leds[] that contains multiple values, but to be handled as one chunk.
Logged

Netherlands
Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi kf2qd,
thx for your reply.

The thing I call struct is probably a function...  smiley-red
If you look at the entire code, you can tell that it's a clock.
The easy part about the original LPD6803 lib is that it's easy to use for my purpose.

At 09:00 the 45th led goes red.
strip.setPixelColor((h, 255, 0, 0);

The FastSPI_led lib is more complicated (to me).
I don't know how to rewrite the struct and/or array(?) and turn it into a function just like the original one.
Try to look at it like this:
I have a new lib (FastSPI_led) and I have the original skecth using different functions.
As far as I now understand the strcut needs to be redefined and the function too.
Am I correct? (still learning...!)
what I need looks something like    FastSPI_led.setPixelColor((h, 255, 0, 0);     smiley-lol

Is someone (or are you) willing to have a closer look at the new lib and the orginal clock sketch?
As mentioned before, it's complicated for me because my mind and eyes don't work well together.
Don't mind the spelling either. I'm Dutch smiley

I attached the sketch before I started to include the fastspi lib.
The fastspi lib cab be found at Google's http://code.google.com/p/fastspi/

* LPD6830_clock.pde (5.23 KB - downloaded 4 times.)
Logged

Pages: [1]   Go Up
Jump to: