Go Down

Topic: A little help please (Read 763 times) previous topic - next topic

ktnch

Feb 12, 2011, 11:03 pm Last Edit: Feb 12, 2011, 11:09 pm by ktnch Reason: 1
Got this sketch for controlling 3 banks off leds with pwm dinning, i need to amend the sketch i have to incorporate the second sketch could some on please help, i am guessing all the rtc from the first sketch needs replacing with the second sketch

#
//Marine Led Controller v1
//created by ReeferGeek 12-02-11.


#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

int whiteFadeOn = 0;
int whiteFadeOff = 0;
int blueFadeOn = 0;
int blueFadeOff = 0;
int violetFadeOn = 0;
int violetFadeOff = 0;

int whiteLedPin = 9; // White Led chain linked to Digital pin9
int blueLedPin = 10; // Blue Led chain linked to Digital pin10
int violetLedPin = 11; // violet led chain linked to digital pin 11

int blueFadeValue = 0;
int whiteFadeValue = 0;
int violetFadeValue = 0;

int userDefMaxWhite = 100; //0-100 user defines the max intensity of the leds
int userDefMaxBlue = 100; //0-100 user defines the max intensity of the leds
int userDefMaxViolet = 100; // 0-100 user defines the max intensity of the leds
int fadeMaxWhite; //variable used when converthing userDefMaxWhite into a PWM value
int fadeMaxBlue; //variable used when converthing userDefMaxBlue into a PWM value
int fadeMaxViolet; //variable used when converting userdefmax into  PWM Value

int hour;//variable used for the RTC timed event.
int minute;//variable used for the RTC timed event.

int counter = 0;//counter for PWN

void setup(){
 Serial.begin(9600); //not needed in end product
 rtcStart();
}





void loop(){
 debug(); //not needed in end product
 maxCalc();
 ledFade();
 rtcCatch();  
}

void ledFade(){  
 if(counter < 60){
   counter++;
   }
 else{
   
   if(whiteFadeOn == 1){
     if(whiteFadeValue <= fadeMaxWhite) {
       analogWrite(whiteLedPin, whiteFadeValue);      
       whiteFadeValue = whiteFadeValue+4; // 255/4 = 63.75 i.e. 0-100% takes 63.75 minutes roughly to complete if it takes 1 minutes to complete 1 cycle.
     }

 }
 
   
   if(violetFadeOn == 1){
     if(violetFadeValue <= fadeMaxViolet) {
       analogWrite(violetLedPin, violetFadeValue);      
       violetFadeValue = violetFadeValue+4; // 255/4 = 63.75 i.e. 0-100% takes 63.75 minutes roughly to complete if it takes 1 minutes to complete 1 cycle.
     }


   }

   if(blueFadeOn == 1){
     if(blueFadeValue <= fadeMaxBlue) {
       analogWrite(blueLedPin, blueFadeValue);      
       blueFadeValue = blueFadeValue+4;
    }
   }  
 
   if(blueFadeOff == 1){
     if(blueFadeValue >= 0) {
       analogWrite(blueLedPin, blueFadeValue);      
       blueFadeValue = blueFadeValue-4;
     }
   }
 
   if(whiteFadeOff == 1){
     if(whiteFadeValue >= 0) {
       analogWrite(whiteLedPin, whiteFadeValue);      
       whiteFadeValue = whiteFadeValue-4;
    }

 }
 
   if(violetFadeOff == 1){
     if(violetFadeValue >= 0) {
       analogWrite(violetLedPin, violetFadeValue);      
       violetFadeValue = violetFadeValue-4;
    }


  }
  counter =0;
}
}








void maxCalc(){
 fadeMaxWhite = userDefMaxWhite*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
 fadeMaxBlue = userDefMaxBlue*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
 fadeMaxViolet = userDefMaxViolet*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
}

void rtcCatch(){
 hour = RTC.get(DS1307_HR,false);
 minute = RTC.get(DS1307_MIN,false);
 
 if((hour == 8)&&(minute == 30)){//sets blue on for 08.30
   blueFadeOn = 1;  
 }
 
 if((hour ==10)&&(minute == 30)){//sets white on for 10.30
   whiteFadeOn = 1;  
 }

 if((hour == 21)&&(minute == 00)){//sets violet on  for 21.00
   violetFadeOn = 1;  
 }
 
 
 if((hour ==20)&&(minute == 0)){//sets for 20:00
   whiteFadeOff = 1;  
 }
 
 if((hour ==10)&&(minute == 0)){//sets for 10:00
   violetFadeOff = 1;  
 }

 if((hour ==21)&&(minute == 00)){//sets for 21:00
   blueFadeOff = 1;  
 }
}

void debug(){ //not needed in end product
 Serial.print(RTC.get(DS1307_HR,true)); Serial.print(":"); Serial.print(RTC.get(DS1307_MIN,false));Serial.print(":");Serial.print(RTC.get(DS1307_SEC,false));
 Serial.print("      Counter:");    
 Serial.print(counter);
 Serial.print("      ");
 Serial.print(RTC.get(DS1307_DATE,false));Serial.print("/"); Serial.print(RTC.get(DS1307_MTH,false));Serial.print("/");Serial.print(RTC.get(DS1307_YR,false));Serial.print(" "); Serial.print(RTC.get(DS1307_DOW,false));
 
 Serial.print(" Blue LED state:");Serial.print(blueFadeOn);Serial.print(blueFadeOff);Serial.print("   PWM Value:");Serial.print(blueFadeValue);
 Serial.print("      ");
 
 Serial.print("White LED state:");Serial.print(whiteFadeOn);Serial.print(whiteFadeOff);Serial.print("   PWM Value");Serial.print(whiteFadeValue);
 Serial.print("      ");
 
 Serial.print("Violet LED state:");Serial.print(violetFadeOn);Serial.print(violetFadeOff);Serial.print("   PWM Value");Serial.print(violetFadeValue);
 Serial.println();
 delay(1000);
}

void rtcStart(){
 RTC.stop();
 RTC.set(DS1307_SEC,00);        //set the seconds
 RTC.set(DS1307_MIN,34);     //set the minutes
 RTC.set(DS1307_HR,9);       //set the hours
 RTC.set(DS1307_DOW,06);       //set the day of the week
 RTC.set(DS1307_DATE,12);       //set the date
 RTC.set(DS1307_MTH,02);        //set the month
 RTC.set(DS1307_YR,11);         //set the year
 RTC.start();  
}






ktnch

Now i need to merge this one with the top code so i can use my data logger


#include <avr/pgmspace.h>
#include <sensor_pff.h>
#include <DS1302.h>

#include "dht11.c"

SENSOR_PFF sd;
DS1302 ds1302;
byte sensor_start = 0;
RTC rtc;

char buff[128];   /* i/o buffer */

static void get_line (char *buff, byte len)
{
   byte c;
   int idx = 0;


   for (;;) {
      if(Serial.available()){
         c = Serial.read();
         if (c == '\r') break;
         if ((c == '\b') && idx) {
            idx--; Serial.print(c);
         }
         if (((byte)c >= ' ') && (idx < len - 1)) {
            buff[idx++] = c; Serial.print(c);
         }
      }
   }
   buff[idx] = 0;
   Serial.print(c);
   Serial.print('\n');
}

/* convert string "<year> <date> <mon> <mday> <hour> <min> <sec>"  to RTC value, return 1 if successful */

byte convert_RTC(char *ptr, RTC* rtc_p)
{
   int val[7];
  byte i = 0;
   int value=0;

   while(*ptr++ && i<7)
   {
      if( *ptr >='0' &&  *ptr<='9' )
         value = value * 10 + *ptr - '0';   
      else if (*ptr == ' '|| !*ptr){
         val[i++] = value;
         value = 0;
      }
   
   }
   
         
   if (i==7) {
         rtc_p->year = (unsigned int) val[0];
      rtc_p->wday = (byte) val[1];
      rtc_p->month = (byte) val[2];
      rtc_p->mday = (byte) val[3];
      rtc_p->hour = (byte) val[4];
      rtc_p->min = (byte)val[5];
      rtc_p->sec = (byte) val[6];
      return 1;
   }
   else return 0;
}
      
   
void setup()
{
   byte res;
   Serial.begin(57600);
  DHT11_init();
   sprintf_P(buff, PSTR("\nRTC datalog shield DEMO\n"));
   Serial.println(buff);   
   Serial.print('>');
}



void loop()
{

   char *ptr;
   long p1, p2;
   byte res;
  unsigned short w;
   
  if(Serial.available() == FALSE){

   if(sensor_start == TRUE){
     byte hum_int=0, hum_dec=0, temp_int=0, temp_dec=0;
     int ret;
          ret = DHT11_read(&hum_int, &hum_dec, &temp_int, &temp_dec);
          ds1302.gettime(&rtc);
   
     sprintf_P(buff, PSTR("%u/%u/%u, %u, %02u:%02u:%02u, hum= %02u.%02u, temp = %02u.%02u\n"), rtc.year, rtc.month, rtc.mday, rtc.wday, rtc.hour, rtc.min, rtc.sec, hum_int, hum_dec, temp_int, temp_dec);
     res = pf_write(buff, strlen(buff), &w);   /* Write data to the file */
     if (res != FR_OK) { Serial.println("write err\n"); }
     else Serial.print('.');
     delay(2000);
    
   }   
   }
  else
   {
     get_line(buff, sizeof(buff));
     ptr = buff;
   
     switch (*ptr++) {
       case 'm':   // mount SD card
          res = disk_initialize();
          if (!res)
              res = pf_mount(&sd.fs);
      if(res)
              sprintf_P(buff, PSTR("SD CARD mount error"));
           else
               sprintf_P(buff, PSTR("SD CARD mount OK"));
              Serial.println(buff);
              break;
      case 't' :   /* t [<year> <date> <mon> <mday> <hour> <min> <sec>] */
            
      if (convert_RTC(ptr, &rtc)) {
        ds1302.settime(&rtc);
          }
         ds1302.gettime(&rtc);
         sprintf_P(buff, PSTR("%u/%u/%u, %u, %02u:%02u:%02u"), rtc.year, rtc.month, rtc.mday, rtc.wday, rtc.hour, rtc.min, rtc.sec);
         Serial.println(buff);
         break;
   case 's':    /* start datalog */
              if(sensor_start == FALSE) sensor_start = TRUE;
         else sensor_start = FALSE;
              break;
         
       case 'f':      /* open a file */
         while(*ptr == ' ') ptr++;
         res = pf_open(ptr);
              if(res)
                sprintf_P(buff, PSTR("open file Error"));
              else
                sprintf_P(buff, PSTR("open file OK"));
               Serial.println(buff);
           break;
       case 'r':   /* read sensor value */
      break;

   }
     Serial.print('>');
   }
}


ktnch

After we have sorted this out i need to include a pwm fan driven from a temp input also a lcd touch screen sketch, any one up for helping

Thank you again


Kev

Groove

My scrolling finger hurts; could you go back and edit your first two posts and put the code into a proper box (use the # icon on the editor toolbar)
Per Arduino ad Astra

WilliamK Govinda

Indeed, that will also remove the  8) smiles from the code. ;-)

Wk

ktnch

ah sorry thats how you do it, Now tried this code i thought it was all fine but when its dimming back down it get stuck on 248 pwm on both white and blues any ideas how i can amend

Code: [Select]
//Marine Led Controller v1
//created by ReeferGeek 12-02-11.


#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>

int whiteFadeOn = 0;
int whiteFadeOff = 0;
int blueFadeOn = 0;
int blueFadeOff = 0;
int violetFadeOn = 0;
int violetFadeOff = 0;

int whiteLedPin = 9; // White Led chain linked to Digital pin9
int blueLedPin = 10; // Blue Led chain linked to Digital pin10
int violetLedPin = 11; // violet led chain linked to digital pin 11

int blueFadeValue = 0;
int whiteFadeValue = 0;
int violetFadeValue = 0;

int userDefMaxWhite = 100; //0-100 user defines the max intensity of the leds
int userDefMaxBlue = 100; //0-100 user defines the max intensity of the leds
int userDefMaxViolet = 100; // 0-100 user defines the max intensity of the leds
int fadeMaxWhite; //variable used when converthing userDefMaxWhite into a PWM value
int fadeMaxBlue; //variable used when converthing userDefMaxBlue into a PWM value
int fadeMaxViolet; //variable used when converting userdefmax into  PWM Value

int hour;//variable used for the RTC timed event.
int minute;//variable used for the RTC timed event.

int counter = 0;//counter for PWN

void setup(){
  Serial.begin(9600); //not needed in end product
  rtcStart();
}





void loop(){
  debug(); //not needed in end product
  maxCalc();
  ledFade();
  rtcCatch(); 
}

void ledFade(){ 
  if(counter < 60){
    counter++;
    }
  else{
   
    if(whiteFadeOn == 1){
      if(whiteFadeValue <= fadeMaxWhite) {
        analogWrite(whiteLedPin, whiteFadeValue);     
        whiteFadeValue = whiteFadeValue+4; // 255/4 = 63.75 i.e. 0-100% takes 63.75 minutes roughly to complete if it takes 1 minutes to complete 1 cycle.
      }

  }
 
   
    if(violetFadeOn == 1){
      if(violetFadeValue <= fadeMaxViolet) {
        analogWrite(violetLedPin, violetFadeValue);     
        violetFadeValue = violetFadeValue+4; // 255/4 = 63.75 i.e. 0-100% takes 63.75 minutes roughly to complete if it takes 1 minutes to complete 1 cycle.
      }


    }

    if(blueFadeOn == 1){
      if(blueFadeValue <= fadeMaxBlue) {
        analogWrite(blueLedPin, blueFadeValue);     
        blueFadeValue = blueFadeValue+4;
     }
    } 
 
    if(blueFadeOff == 1){
      if(blueFadeValue >= 0) {
        analogWrite(blueLedPin, blueFadeValue);     
        blueFadeValue = blueFadeValue-4;
      }
    }
 
    if(whiteFadeOff == 1){
      if(whiteFadeValue >= 0) {
        analogWrite(whiteLedPin, whiteFadeValue);     
        whiteFadeValue = whiteFadeValue-4;
     }

  }
 
    if(violetFadeOff == 1){
      if(violetFadeValue >= 0) {
        analogWrite(violetLedPin, violetFadeValue);     
        violetFadeValue = violetFadeValue-4;
     }


   }
   counter =0;
}
}








void maxCalc(){
  fadeMaxWhite = userDefMaxWhite*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
  fadeMaxBlue = userDefMaxBlue*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
  fadeMaxViolet = userDefMaxViolet*2.55;//converts user defined percentage (0-100) into a PWM value (0-255)
}

void rtcCatch(){
  hour = RTC.get(DS1307_HR,false);
  minute = RTC.get(DS1307_MIN,false);
 
  if((hour == smiley-cool&&(minute == 30)){//sets blue on for 08.30
    blueFadeOn = 1; 
  }
 
  if((hour ==10)&&(minute == 30)){//sets white on for 10.30
    whiteFadeOn = 1; 
  }

  if((hour == 21)&&(minute == 00)){//sets violet on  for 21.00
    violetFadeOn = 1; 
  }
 
 
  if((hour ==20)&&(minute == 0)){//sets for 20:00
    whiteFadeOff = 1; 
  }
 
  if((hour ==10)&&(minute == 0)){//sets for 10:00
    violetFadeOff = 1; 
  }

  if((hour ==21)&&(minute == 00)){//sets for 21:00
    blueFadeOff = 1; 
  }
}

void debug(){ //not needed in end product
  Serial.print(RTC.get(DS1307_HR,true)); Serial.print(":"); Serial.print(RTC.get(DS1307_MIN,false));Serial.print(":");Serial.print(RTC.get(DS1307_SEC,false));
  Serial.print("      Counter:");   
  Serial.print(counter);
  Serial.print("      ");
  Serial.print(RTC.get(DS1307_DATE,false));Serial.print("/"); Serial.print(RTC.get(DS1307_MTH,false));Serial.print("/");Serial.print(RTC.get(DS1307_YR,false));Serial.print(" "); Serial.print(RTC.get(DS1307_DOW,false));
 
  Serial.print(" Blue LED state:");Serial.print(blueFadeOn);Serial.print(blueFadeOff);Serial.print("   PWM Value:");Serial.print(blueFadeValue);
  Serial.print("      ");
 
  Serial.print("White LED state:");Serial.print(whiteFadeOn);Serial.print(whiteFadeOff);Serial.print("   PWM Value");Serial.print(whiteFadeValue);
  Serial.print("      ");
 
  Serial.print("Violet LED state:");Serial.print(violetFadeOn);Serial.print(violetFadeOff);Serial.print("   PWM Value");Serial.print(violetFadeValue);
  Serial.println();
  delay(1000);
}

void rtcStart(){
  RTC.stop();
  RTC.set(DS1307_SEC,00);        //set the seconds
  RTC.set(DS1307_MIN,34);     //set the minutes
  RTC.set(DS1307_HR,9);       //set the hours
  RTC.set(DS1307_DOW,06);       //set the day of the week
  RTC.set(DS1307_DATE,12);       //set the date
  RTC.set(DS1307_MTH,02);        //set the month
  RTC.set(DS1307_YR,11);         //set the year
  RTC.start(); 
}

ktnch

And then when we have fixed the bug in the above program i need to add this code unless you can work out which pin the rtc is linked to

Code: [Select]
#include <avr/pgmspace.h>
#include <sensor_pff.h>
#include <DS1302.h>

#include "dht11.c"

SENSOR_PFF sd;
DS1302 ds1302;
byte sensor_start = 0;
RTC rtc;

char buff[128];   /* i/o buffer */

static void get_line (char *buff, byte len)
{
   byte c;
   int idx = 0;


   for (;smiley-wink {
      if(Serial.available()){
         c = Serial.read();
         if (c == '\r') break;
         if ((c == '\b') && idx) {
            idx--; Serial.print(c);
         }
         if (((byte)c >= ' ') && (idx < len - 1)) {
            buff[idx++] = c; Serial.print(c);
         }
      }
   }
   buff[idx] = 0;
   Serial.print(c);
   Serial.print('\n');
}

/* convert string "<year> <date> <mon> <mday> <hour> <min> <sec>"  to RTC value, return 1 if successful */

byte convert_RTC(char *ptr, RTC* rtc_p)
{
   int val[7];
  byte i = 0;
   int value=0;

   while(*ptr++ && i<7)
   {
      if( *ptr >='0' &&  *ptr<='9' )
         value = value * 10 + *ptr - '0';   
      else if (*ptr == ' '|| !*ptr){
         val[i++] = value;
         value = 0;
      }
   
   }
   
         
   if (i==7) {
         rtc_p->year = (unsigned int) val[0];
      rtc_p->wday = (byte) val[1];
      rtc_p->month = (byte) val[2];
      rtc_p->mday = (byte) val[3];
      rtc_p->hour = (byte) val[4];
      rtc_p->min = (byte)val[5];
      rtc_p->sec = (byte) val[6];
      return 1;
   }
   else return 0;
}
     
   
void setup()
{
   byte res;
   Serial.begin(57600);
  DHT11_init();
   sprintf_P(buff, PSTR("\nRTC datalog shield DEMO\n"));
   Serial.println(buff);   
   Serial.print('>');
}



void loop()
{

   char *ptr;
   long p1, p2;
   byte res;
  unsigned short w;
   
  if(Serial.available() == FALSE){

   if(sensor_start == TRUE){
     byte hum_int=0, hum_dec=0, temp_int=0, temp_dec=0;
     int ret;
          ret = DHT11_read(&hum_int, &hum_dec, &temp_int, &temp_dec);
          ds1302.gettime(&rtc);
   
     sprintf_P(buff, PSTR("%u/%u/%u, %u, %02u:%02u:%02u, hum= %02u.%02u, temp = %02u.%02u\n"), rtc.year, rtc.month, rtc.mday, rtc.wday, rtc.hour, rtc.min, rtc.sec, hum_int, hum_dec, temp_int, temp_dec);
     res = pf_write(buff, strlen(buff), &w);   /* Write data to the file */
     if (res != FR_OK) { Serial.println("write err\n"); }
     else Serial.print('.');
     delay(2000);
   
   }   
   }
  else
   {
     get_line(buff, sizeof(buff));
     ptr = buff;
   
     switch (*ptr++) {
       case 'm':   // mount SD card
          res = disk_initialize();
          if (!res)
              res = pf_mount(&sd.fs);
      if(res)
              sprintf_P(buff, PSTR("SD CARD mount error"));
           else
               sprintf_P(buff, PSTR("SD CARD mount OK"));
              Serial.println(buff);
              break;
      case 't' :   /* t [<year> <date> <mon> <mday> <hour> <min> <sec>] */
           
      if (convert_RTC(ptr, &rtc)) {
        ds1302.settime(&rtc);
          }
         ds1302.gettime(&rtc);
         sprintf_P(buff, PSTR("%u/%u/%u, %u, %02u:%02u:%02u"), rtc.year, rtc.month, rtc.mday, rtc.wday, rtc.hour, rtc.min, rtc.sec);
         Serial.println(buff);
         break;
   case 's':    /* start datalog */
              if(sensor_start == FALSE) sensor_start = TRUE;
         else sensor_start = FALSE;
              break;
         
       case 'f':      /* open a file */
         while(*ptr == ' ') ptr++;
         res = pf_open(ptr);
              if(res)
                sprintf_P(buff, PSTR("open file Error"));
              else
                sprintf_P(buff, PSTR("open file OK"));
               Serial.println(buff);
           break;
       case 'r':   /* read sensor value */
      break;

   }
     Serial.print('>');
   }
}



Go Up