Go Down

Topic: I thought I understood arrays? (Read 340 times) previous topic - next topic

SteveHi

I guess I only understand simplistic arrays...

I was hoping someone would be kind enough to double check my code. I have been over it and over it for several hours, and by all accounts it should be working. I think. I am hoping a fresh pair of eyes will spot what I am missing.

I am trying to write a Sunrise/Sunset simulation using 9 LED channels on the pwm pins. I wrote a matrix to call start times, ramp up times, ramp down times, and led intensities. I also wrote a function to determine where the current point the lighting matrix is at.

I believe my mistake lies somewhere in my array/index calls, but I can't seem to locate it.

The follow is the code I wrote, and it is not giving accurate lighting results. It could also be a RTC issue.


Code: [Select]
//*******Included Libraries*******//
#include <ITDB02_Touch.h>
#include <avr/pgmspace.h>
#include <UTFT.h>
#include <tinyFAT.h>
#include <UTFT_tinyFAT.h>


#include <RTClib.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//********    Declare Touch/LCD/RTC    **********
UTFTtf  myGLCD(ITDB32S, 38, 39, 40, 41);
ITDB02_Touch  myTouch(46,45,44,43,42);
RTC_DS1307 RTC;


//********   Call Fonts    **********
extern uint8_t Sinclair_M[];
extern uint8_t Sinclair_S[];
extern uint8_t SevenSegNumFontPlus[];


//*******   Declare 1-Wire for TEMP sensors  ********
#define ONE_WIRE_BUS 1
#define TEMPERATURE_PRECISION 9
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature Sensors(&oneWire);

//***************   Sunrise/sunset Variables **************************//

const int CHANNELS = 9;
const int MAXPERIODS = 3;



//###   Clock Varibles ################################
boolean AM_PM;
int Hours;
int Minutes;
int Seconds;
int Day;
int Month;
int Year;
int Weekday;
String Current_time;
int Time_Format = 2;
boolean Daytime = true;
boolean Was_day;

//### LED Variables ############################

const int Wht_Ch1 = 3;
const int RBlue_Ch1 = 5;
const int Blue_Ch1 = 7;
const int Wht_Ch2 = 8;
const int RBlue_Ch2 = 9;
const int Blue_Ch2 = 10;
const int Wht_Ch_3 = 11;
const int RBlue_Ch3 = 12;
const int UV_Ch1 = 13;

//###   Sunrise/SunSet Variables #########################
boolean Sun_rise = true;
boolean Equinox_mode = true;
int Sunrise_Hour;
int Sunrise_Min;
int Sunrise_Sec;
int Sunset_Hour;
int Sunset_Min;
int Sunrise_length = 0;
int Sun_level;

int sunMatrix[CHANNELS][MAXPERIODS][5] =
{
  {
    (0, 0, 9, 15, 0),
    (9, 45, 18, 45, 255),    //Wht_ch1
    (19, 15, 24, 0, 0),
  },{
    (0, 0, 8, 0, 0),
    (8, 30, 20, 0, 255),     //RBlue_Ch1
    (20, 30, 24, 0, 0),
  },{
    (0, 0, 8, 45, 0),
    (9, 15, 19, 30, 255),   //Blue Ch1
    (20, 0, 24, 0, 0),
  },{
    (0, 0, 9, 30, 0),
    (10, 0, 19, 0, 255),    //Wht_Ch2
    (19, 30, 24, 0, 0),
  },{
    (0, 0, 8, 15, 0),
    (8, 45, 20, 15, 255),    //RBlue_Ch2
    (20, 45, 24, 0, 0),
  },{
    (0, 0, 9, 0, 0),
    (9, 30, 19, 45, 255),    //Blue_Ch2
    (8, 15, 24, 0, 0),
  },{
    (0, 0, 9, 45, 0),
    (10, 15, 19, 15, 255),     //Wht_Ch_3
    (19, 30, 24, 0, 0),
  },{
    (0, 0, 8, 30, 0),
    (9, 0, 20, 30, 255),     //RBlue_Ch3
    (21, 0, 24, 0, 0),
  },{
    (0, 0, 7, 45, 64),
    (8, 0, 21, 0 , 128),       //UV_Ch1
    (21, 15, 24, 0, 64),
  }
};

int sunValue[CHANNELS] = {0, 0, 0, 0, 0, 0, 0, 0, 0};   
   

//*****************************************************************************************************
//*****************************************************************************************************
//*******************************************      $Setup      ********************************[#SETUP]
//*****************************************************************************************************
//*****************************************************************************************************

void setup(){

  myGLCD.InitLCD(PORTRAIT);                           //Initiate LCD Screen
  myGLCD.clrScr();
  file.initFAT(SPISPEED_VERYHIGH);                    //Initiate SD Card for FAT16 Lib
  Wire.begin();
  RTC.begin();                                        //Initiate Real Time Clock
 
  myTouch.InitTouch(PORTRAIT);                        //Initiate Touch Screen for Portriat
  myTouch.setPrecision(PREC_HI);

  Sensors.begin();                                                //Begin Dallas 1-Wire Temperature
  pinMode(1, OUTPUT);
  RTC.adjust(DateTime(__DATE__, __TIME__));            //Sets clock to current upload time
 

 
 
  Serial.begin(9600);
  boolean home_screen = true;
  Home_Screen();

 
}

//**************************************************//
/////////************* FUNCTIONS ***********//////////
//**************************************************//

//##################################################//
////////########### Sunrise/Sunset #################//
//##################################################//

void Sunrise_set(long seconds){
 
     for(int channel = 0; channel < CHANNELS; channel ++){  //for each channel
        for(int period = 0; period < MAXPERIODS; period++){  //for each possible period
           long pEnd = GetSeconds(sunMatrix[channel][period][Sunset_Hour], sunMatrix[channel][period][Sunset_Min], 0);
if(pEnd >= seconds) { // Period is currently happening
sunValue[channel] = sunMatrix[channel][period][Sun_level]; // Set light to defined value
break; // Found correct period, don't check the rest
} else { // Period has not yet happened
long pNextStart = GetSeconds(sunMatrix[channel][period+1][Sunrise_Hour], sunMatrix[channel][period+1][Sunrise_Min], 0); // Get next period start time in seconds
if(pNextStart > seconds) { // Currently in between periods
int preVal = sunMatrix[channel][period][Sun_level];     // Old light value
int postVal = sunMatrix[channel][period+1][Sun_level]; // New light value
int tDur = pNextStart - pEnd; // Transition duration
int intensityDiff = postVal - preVal; // Difference in light intesity between periods
if(intensityDiff > 0) { // Intensity increasing
sunValue[channel] = (int)((seconds - pEnd) * ((float)intensityDiff / tDur)); // Light value
} else { // Intensity decreasing
sunValue[channel] = preVal - (int)((seconds - pEnd) * ((float)(0-intensityDiff) / tDur)); // Light value
}
break; // Found correct period, don't check the rest
}
}
}
}
}

long GetSeconds(int hours, int minutes, int seconds) // Convert HH:mm:ss -> Seconds since midnight
{
return ((long)hours * 60 * 60) + (minutes * 60) + seconds ;
}


//############################################################################################################################//
//############################################################################################################################//
//################################################# MAIN LOOP ################################################################//
//############################################################################################################################//
//############################################################################################################################//


void loop(){
   
//This code segment reads the current touch screen data (position pressed by the user) and stores the
//x value and the y value.

  if (myTouch.dataAvailable()){          //Get Curent Screen Data
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    Serial.print(x);
    Serial.print(" / ");
    Serial.println(y);
  }

//#############################################################//
//######################## CLOCK ##############################//
//#############################################################//
// This segment of code starts and draws Clock for all screens

  DateTime now = RTC.now();             //sets Datetime "now" to the current time
  Hours = now.hour();                         //Sets time varibles to Current RTC time
  Minutes = now.minute();
  Seconds = now.second();
  Day = now.day();
  Month = now.month();
  Year = now.year();
  Weekday = now.dayOfWeek();
 
  Draw_Clock(24, 252);
 
 
  //################## sunrise_sunset code ###################//
 
  Sunrise_set(GetSeconds(Hours, Minutes, Seconds));
 
  analogWrite(Wht_Ch1, sunValue[0]);
  analogWrite(RBlue_Ch1, sunValue[1]);
  analogWrite(Blue_Ch1, sunValue[2]);
  analogWrite(Wht_Ch_3, sunValue[3]);
  analogWrite(RBlue_Ch2, sunValue[4]);
  analogWrite(Blue_Ch2, sunValue[5]);
  analogWrite(Wht_Ch_3, sunValue[6]);
  analogWrite(RBlue_Ch3, sunValue[7]);
  analogWrite(UV_Ch1, sunValue[8]);


Any thoughts would be appreciated.

econjack

I can't get the code to compile, but nowhere do I see Sunset_Hour set to a value. The assumption the code is making is that it is 0, but it that what you intended? If it is always 0, there's no need for a variable.

SteveHi

Sorry. In deleting all the navigation code, and other code unnecessary to this section I had forgotten a thing or 2.
Also, I had originally had Sunrise_Min, and others = 0...I have changed that back.

This code should compile for you:
Code: [Select]



//*******Included Libraries*******//
#include <ITDB02_Touch.h>
#include <avr/pgmspace.h>
#include <UTFT.h>
#include <tinyFAT.h>
#include <UTFT_tinyFAT.h>


#include <RTClib.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>

//********    Declare Touch/LCD/RTC    **********
UTFTtf  myGLCD(ITDB32S, 38, 39, 40, 41);
ITDB02_Touch  myTouch(46,45,44,43,42);
RTC_DS1307 RTC;



//*************** Named Sunrise/sunset Variables **************************//

const int CHANNELS = 9;
const int MAXPERIODS = 3;

//########################################################################################################
//###########################################      $Global Varibles      #############################[#VAR]
//########################################################################################################


//###   Touch Screen Varibles  ########################
int x, y;



//###   Clock Varibles ################################

int Hours;
int Minutes;
int Seconds;
int Day;
int Month;
int Year;
int Weekday;
String Current_time;
int Time_Format = 2;
boolean Daytime = true;
boolean Was_day;

//### LED Variables ############################

const int Wht_Ch1 = 3;
const int RBlue_Ch1 = 5;
const int Blue_Ch1 = 7;
const int Wht_Ch2 = 8;
const int RBlue_Ch2 = 9;
const int Blue_Ch2 = 10;
const int Wht_Ch_3 = 11;
const int RBlue_Ch3 = 12;
const int UV_Ch1 = 13;

//###   Sunrise/SunSet Variables #########################

int Sunrise_Hour = 0;
int Sunrise_Min = 0;
int Sunrise_Sec = 0;
int Sunset_Hour = 0;
int Sunset_Min = 0;
int Sunrise_length = 0;
int Sun_level = 0;

int sunMatrix[CHANNELS][MAXPERIODS][5] =
{
  {
    (0, 0, 9, 15, 0),
    (9, 45, 18, 45, 255),    //Wht_ch1
    (19, 15, 24, 0, 0),
  },{
    (0, 0, 8, 0, 0),
    (8, 30, 20, 0, 255),     //RBlue_Ch1
    (20, 30, 24, 0, 0),
  },{
    (0, 0, 8, 45, 0),
    (9, 15, 19, 30, 255),   //Blue Ch1
    (20, 0, 24, 0, 0),
  },{
    (0, 0, 9, 30, 0),
    (10, 0, 19, 0, 255),    //Wht_Ch2
    (19, 30, 24, 0, 0),
  },{
    (0, 0, 8, 15, 0),
    (8, 45, 20, 15, 255),    //RBlue_Ch2
    (20, 45, 24, 0, 0),
  },{
    (0, 0, 9, 0, 0),
    (9, 30, 19, 45, 255),    //Blue_Ch2
    (8, 15, 24, 0, 0),
  },{
    (0, 0, 9, 45, 0),
    (10, 15, 19, 15, 255),     //Wht_Ch_3
    (19, 30, 24, 0, 0),
  },{
    (0, 0, 8, 30, 0),
    (9, 0, 20, 30, 255),     //RBlue_Ch3
    (21, 0, 24, 0, 0),
  },{
    (0, 0, 7, 45, 64),
    (8, 0, 21, 0 , 128),       //UV_Ch1
    (21, 15, 24, 0, 64),
  }
};

int sunValue[CHANNELS] = {0, 0, 0, 0, 0, 0, 0, 0, 0};   
   

//*****************************************************************************************************
//*****************************************************************************************************
//*******************************************      $Setup      ********************************[#SETUP]
//*****************************************************************************************************
//*****************************************************************************************************

void setup(){

  myGLCD.InitLCD(PORTRAIT);                           //Initiate LCD Screen
  myGLCD.clrScr();
  file.initFAT(SPISPEED_VERYHIGH);                    //Initiate SD Card for FAT16 Lib
  Wire.begin();
  RTC.begin();                                        //Initiate Real Time Clock
 

  RTC.adjust(DateTime(__DATE__, __TIME__));            //Sets clock to current upload time
 

 
 
  Serial.begin(9600);



 
}

//**************************************************//
/////////************* FUNCTIONS ***********//////////
//**************************************************//

//##################################################//
////////########### Sunrise/Sunset #################//
//##################################################//

void Sunrise_set(long seconds){
 
     for(int channel = 0; channel < CHANNELS; channel ++){  //for each channel
        for(int period = 0; period < MAXPERIODS; period++){  //for each possible period
           long pEnd = GetSeconds(sunMatrix[channel][period][Sunset_Hour], sunMatrix[channel][period][Sunset_Min], 0);
if(pEnd >= seconds) { // Period is currently happening
sunValue[channel] = sunMatrix[channel][period][Sun_level]; // Set light to defined value
break; // Found correct period, don't check the rest
} else { // Period has not yet happened
long pNextStart = GetSeconds(sunMatrix[channel][period+1][Sunrise_Hour], sunMatrix[channel][period+1][Sunrise_Min], 0); // Get next period start time in seconds
if(pNextStart > seconds) { // Currently in between periods
int preVal = sunMatrix[channel][period][Sun_level];     // Old light value
int postVal = sunMatrix[channel][period+1][Sun_level]; // New light value
int tDur = pNextStart - pEnd; // Transition duration
int intensityDiff = postVal - preVal; // Difference in light intesity between periods
if(intensityDiff > 0) { // Intensity increasing
sunValue[channel] = (int)((seconds - pEnd) * ((float)intensityDiff / tDur)); // Light value
} else { // Intensity decreasing
sunValue[channel] = preVal - (int)((seconds - pEnd) * ((float)(0-intensityDiff) / tDur)); // Light value
}
break; // Found correct period, don't check the rest
}
}
}
}
}

long GetSeconds(int hours, int minutes, int seconds) // Convert HH:mm:ss -> Seconds since midnight
{
return ((long)hours * 60 * 60) + (minutes * 60) + seconds ;
}


//############################################################################################################################//
//############################################################################################################################//
//################################################# MAIN LOOP ################################################################//
//############################################################################################################################//
//############################################################################################################################//


void loop(){
   
//This code segment reads the current touch screen data (position pressed by the user) and stores the
//x value and the y value.

  if (myTouch.dataAvailable()){          //Get Curent Screen Data
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    Serial.print(x);
    Serial.print(" / ");
    Serial.println(y);
  }

//#############################################################//
//######################## CLOCK ##############################//
//#############################################################//
// This segment of code starts and draws Clock for all screens

  DateTime now = RTC.now();             //sets Datetime "now" to the current time
  Hours = now.hour();                 //Sets time varibles to Current RTC time
  Minutes = now.minute();
  Seconds = now.second();
  Day = now.day();
  Month = now.month();
  Year = now.year();
  Weekday = now.dayOfWeek();
 

 
 
  //################## sunrise_sunset code ###################//
 
  Sunrise_set(GetSeconds(Hours, Minutes, Seconds));
 
  analogWrite(Wht_Ch1, sunValue[0]);
  analogWrite(RBlue_Ch1, sunValue[1]);
  analogWrite(Blue_Ch1, sunValue[2]);
  analogWrite(Wht_Ch_3, sunValue[3]);
  analogWrite(RBlue_Ch2, sunValue[4]);
  analogWrite(Blue_Ch2, sunValue[5]);
  analogWrite(Wht_Ch_3, sunValue[6]);
  analogWrite(RBlue_Ch3, sunValue[7]);
  analogWrite(UV_Ch1, sunValue[8]);
}


Thanks for looking!

SteveHi

Maybe someone could just answer this for me.

In this statement

int sunMatrix [CHANNELS][MAXPERIODS][5]

CHANNELS is the index to array for all included PWM pins in the matrix
MAXPERIODS is the index to array for timing periods within the matrix
5 = ?
What does the 5 index?

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