Apologies for the unfriendly file format, and thanks CrossRoads for posting a PNG version of the schematic.
As far as the code goes, it's as simple as this:
int ledPins[] = {5, 6, 9, 10, 11, 13};
void setup() {
Serial.begin(9600);
int pwmValue = 20;
Serial.println(pwmValue);
for (int i = 0; i < 6; i++) {
Serial.println(ledPins[i]);
analogWrite(ledPins[i], pwmValue);
}
}
void loop() {
// nothing happens in loop
}
The PWM pins that are connected are:
Arduino -- Port/bit -- Atmega32u4 pin
D5 ------- PC6 ------ Pin 31
D6 ------- PD7 ------ Pin 27
D9 ------- PB5 ------ Pin 29
D10 ------ PB6 ------ Pin 30
D11 ------ PB7 ------ Pin 12
D13 ------ PC7 ------ Pin32
The code I currently have flashed is below as well, which where i first noticed the problem, and then it persisted with the debug code above:
#include <Time.h>
#include <Wire.h>
#define SLAVE_ADDRESS 0x04
const float pi = 3.1415926535897932384;
int pwmOuts[] = {5, 6, 9, 10, 11, 13};
int maxBrightness = 120; // Absolute maximum of the LEDs
int brightness = 0; // how bright the LED is
int number = 0;
int constBrightness = 0; // no sun-cycle, constant value == maxBrightness
int statusArray[6] = {}; // status array to control settings, all zeros == program will run with defaults
void setup() {
// Set the LED PWM "bus" to output
for (int i = 0; i < 7; i++) {
pinMode(pwmOuts[i], OUTPUT);
}
setTime(8, 00, 00, 30, 10, 2013);
Serial.begin(9600);
Wire.begin(SLAVE_ADDRESS); // initialize i2c as slave
// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}
void loop() {
// Serial Time information
if(timeStatus() == timeNotSet)
Serial.println("waiting for sync message");
else
digitalClockDisplay();
// set the brightness
if ( constBrightness == 1 ) {
brightness = maxBrightness;
} else {
float theta = ((hour() + minute()/60.0 - 7)/(12.0)) * pi;
brightness = maxBrightness * sin(theta);
}
if (brightness > maxBrightness) {
brightness = maxBrightness;
}
if (brightness < 0) {
brightness = 0;
}
//Serial.println(brightness);
//bitBangPWM(brightness);
for (int i = 0; i < 7; i++) {
Serial.print(pwmOuts[i]);
analogWrite(pwmOuts[i], brightness);
}
delay(1000); // We don't need things to update this often
}
void digitalClockDisplay(){
// digital clock display of the time
Serial.print(hour());
printDigits(minute());
printDigits(second());
Serial.print(" ");
Serial.print(day());
Serial.print(" ");
Serial.print(month());
Serial.print(" ");
Serial.print(year());
Serial.println();
}
void parseRPI(int arr[]) {
// byte 1 = set time yes/no
// byte 2 = hour to set
// byte 3 = set max brightness yes/no
// byte 4 = max brightness
// byte 5 = force brightness; value = max brightness; yes/no
if ( arr[0] == 1 ) {
setTime(arr[1], arr[2], 00, 1, 12, 2013);
}
if ( arr[3] == 1 ) {
maxBrightness = arr[4];
}
if ( arr[5] == 1) {
constBrightness = 1;
} else {
constBrightness = 0;
}
}
void printDigits(int digits){
// utility function for digital clock display: prints preceding colon and leading 0
Serial.print(":");
if(digits < 10)
Serial.print('0');
Serial.print(digits);
}
// callback for received data
void receiveData(int byteCount){
int cnt = 0;
while(Wire.available()) {
number = Wire.read();
Serial.print("data received: ");
Serial.println(number);
statusArray[cnt] = number;
cnt++;
//analogWrite(13, number);
}
// Update the program variables
parseRPI(statusArray);
}
// callback for sending data -- This function isn't ready for multi-byte data sets
void sendData(){
Wire.write(number);
}
Since I'm using i2c to communicate between my RaspberryPi and Arduino, I am not using D3 as a PWM output.