Go Down

Topic: rewrite function from struct (Read 555 times) previous topic - next topic

Jan 31, 2012, 12:14 pm Last Edit: Jan 31, 2012, 09:18 pm by RogerMoerdijk Reason: 1
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: [Select]
strip.setPixelColor(h, 0, 0, brightness);

should become something like this: (0=led nr and should become h, m and s variable)
Code: [Select]
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: [Select]
// 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();
}

johnwasser

You probably want an array of structures:
Code: [Select]

#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: [Select]

#define NUM_LEDS 60
struct CRGB { unsigned char r; unsigned char g; unsigned char b; } eds[NUM_LEDS];
Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

kf2qd

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.

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