I've been working on a project that uses the adafruit 16 channel PWM/servo I2C driver. so far I've managed to set up my code so that it will work correctly with a few functions: on, off, color. (color function uses an array vs. you having to type the 255 code multiple times) all the subroutines function normal, but I'm trying to turn it into a library as the project is being designed for someone else and they would like a library that they could just reference the commands from instead of typing all the code.
I've been following a Morse code example library tutorial (which i got working)
my "h" file (this was typed in notepad, not sure if this can cause an issue; although it didn't for the more example)
#ifndef i2cRGB_h
#define i2cRGB_h
#include "Arduino.h"
#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
class RGB
{
public:
void on(uint8_t c,uint8_t r,uint8_t g,uint8_t b);
void color(uint8_t c, int color[3]);
void off(uint8_t c);
int black[3] = { 0, 0, 0 };
int white[3] = { 255, 255, 255 };
int red[3] = { 255, 0, 0 };
int green[3] = { 0, 255, 0 };
int blue[3] = { 0, 0, 255 };
int yellow[3] = { 175, 200, 0 };
int orange[3] = { 240, 70, 0 };
int purple[3] = { 170, 0, 200 };
private:
int r = 0;
int g = 0;
int b = 0;
int c = 0;
int R = 0;
int G = 0;
int B = 0;
};
#endif
my "cpp" file:
#include "Arduino.h"
#include "i2cRGB.h"
#include "Wire.h"
#include "Adafruit_PWMServoDriver.h"
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
pwm.begin();
pwm.setPWMFreq(1000);
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 255, 255, 255 };
int red[3] = { 255, 0, 0 };
int green[3] = { 0, 255, 0 };
int blue[3] = { 0, 0, 255 };
int yellow[3] = { 175, 200, 0 };
void RGB::off(uint8_t c){
pwm.setPWM(0+(3*c),0,0); // off
pwm.setPWM(1+(3*c),0,0); // off
pwm.setPWM(2+(3*c),0,0); // off
}
void RGB::on(uint8_t c,uint8_t r,uint8_t g,uint8_t b){
R = r;
G = g;
B = b;
R = map(r,0,255,0,4095);
G = map(g,0,255,0,4095);
B = map(b,0,255,0,4095);
pwm.setPWM(0+(3*c),0,R); //red
pwm.setPWM(1+(3*c),0,G); //green
pwm.setPWM(2+(3*c),0,B); //blue
}
void RGB::color(uint8_t c, int color[3]) {
int r = color[0];
int g = color[1];
int b = color[2];
R = r;
G = g;
B = b;
R = map(r,0,255,0,4095);
G = map(g,0,255,0,4095);
B = map(b,0,255,0,4095);
pwm.setPWM(0+(3*c),0,R); //red
pwm.setPWM(1+(3*c),0,G); //green
pwm.setPWM(2+(3*c),0,B); //blue
}
when loading the following code in arduino and trying to compile i receive an error
Arduino: 1.6.13 (Windows 7), Board: "Arduino Nano, ATmega328"
In function 'void loop()':
i2c_library_test:8: error: expected unqualified-id before '.' token
RGB.on();
^
exit status 1
expected unqualified-id before '.' token
#include <i2cRGB.h>
void setup() {}
void loop() {
Rgb.on();
}
any thoughts or suggestions would be appreciated.
in case it matters this is the functional code, I've been testing and debugging functions hence why things are commented out
#include <Wire.h>
#include <Adafruit_PWMServoDriver.h>
Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver();
//Adafruit_PWMServoDriver pwm = Adafruit_PWMServoDriver(0x41);
// color varibles
int r = 0;
int g = 0;
int b = 0;
int c = 0;
int R = 0;
int G = 0;
int B = 0;
// Color arrays
int black[3] = { 0, 0, 0 };
int white[3] = { 255, 255, 255 };
int red[3] = { 255, 0, 0 };
int green[3] = { 0, 255, 0 };
int blue[3] = { 0, 0, 255 };
int yellow[3] = { 175, 200, 0 };
void setup() {
//Serial.begin(9600);
pwm.begin();
pwm.setPWMFreq(1000); //setting frequency of pwm driver; 1000HZ
}
void loop() {
//off(0); //off channel 0
//off(1); //off channel 0
// color(0,red); //sets channel 0 to red
// color(1,green); //sets channel 1 to green
// delay(1000);
// color(0,blue);
// color(1,red);
// delay(1000);
// on(0,0,0,0); // sets channel 0 to black; channel # then R,G,B value
// delay(1000);
// on(0,0,0,0);
// delay(1000);
}
void off(uint8_t c){
// the math is used on the first digit to set the correct channel on the PWM controller to control a mosfet, in this case RGB uses 3 mosfets , allows the user to set all RGB
// string without using multiple lines of code
//second digit goes unchanged
//third digit is the PWM level for the channel on the PWM driver, it is a 16 bit driver, so 0-4095
pwm.setPWM(0+(3*c),0,0); // off
pwm.setPWM(1+(3*c),0,0); // off
pwm.setPWM(2+(3*c),0,0); // off
}
void on(uint8_t c,uint8_t r,uint8_t g,uint8_t b){
R = r;
G = g;
B = b;
R = map(r,0,255,0,4095); // changes from a 255 value to a 16 bit value as the PWM driver outputs a 16bit pulse
G = map(g,0,255,0,4095);
B = map(b,0,255,0,4095);
//Serial.println(R);
//Serial.println(G);
//Serial.println(B);
pwm.setPWM(0+(3*c),0,R); //red
pwm.setPWM(1+(3*c),0,G); //green
pwm.setPWM(2+(3*c),0,B); //blue
}
void color(uint8_t c, int color[3]) {
int r = color[0]; // more varibles to extract the array numbers
int g = color[1];
int b = color[2];
R = r;
G = g;
B = b;
R = map(r,0,255,0,4095);
G = map(g,0,255,0,4095);
B = map(b,0,255,0,4095);
pwm.setPWM(0+(3*c),0,R); //red
pwm.setPWM(1+(3*c),0,G); //green
pwm.setPWM(2+(3*c),0,B); //blue
}