I made a simple diode array cell by mounting different colored LED's side-by-side in a drilled-out PVC block. Each LED is paired to a photodiode. So 3 LEDs (blue, green and red) are illuminated in sequence. Only one is lit at a time to prevent cross "contamination". The PD output is scaled to 1 byte and, presently, printed on the IDE monitor. I would like to send to another board by I2C.
Why? ...We got into dyeing garments and just about anything else we could catch. But often what we thought we'd get came out quite different hue/intensity, hard to explain but if you've ever done much fabric dying you'll know what i mean. The purpose of this project is to try quantifing the color intensity of dyes and mixtures of dyes so that we can have a better way of predicting final colors and intensity of the final product.
Now that it is working, I'd like to go one step further and send the data by I2C to a Master Uno located 2 meters away from the Slave.
The reason I've come here is to ask how to structure the Slave sender sketch to send the data. Here is my Mini spectrophotometer sketch of which I am unduly proud:
/*Sequentially turns on 3 different color leds mounted in a plastic cell. Each LED burns for 1s
* then the output from a photodiode (digikey SHF213) mounted opposite the LED
* is read via A1 through A3 respectively. The output is mapped to 255 (1 byte).
* The data is sent to the serial monitor.
*/
// declare constants that won't change
const int LED476 = 9; //i/o pin connect to 476nm LED
const int LED538 = 10; //i/o pin connect to 538 LED
const int LED600 = 11; //i/o pin connect to 600 LED
int bluePD = A1; // analog input pin A1 to 476 detector PD
int blueIN = 0; // establish the variable to hold 476 readings
int greenPD = A2; // analog input pin A2 to 538 detector PD
int greenIN = 0; // variable to hold 538 readings
int redPD = A3; // analog input pin A3 to 600 detector PD
int redIN = 0; // variable to hold 600 readings
// variables that will change
int LED476STATE = LOW;
int LED538STATE = LOW;
int LED600STATE = LOW;
int sval;
int Analogpin;
void setup() {
//set LED pin as output
pinMode(9,OUTPUT);
pinMode(10,OUTPUT);
pinMode(11,OUTPUT);
Serial.begin(9600);
}
void loop() {
Analogpin = 1;
LightLEDs(100,0,0); //476 light intensity is proportional to PWM setting 0 to 255
delay (1000);
int sens = ReadSensor();
Serial.print("476 PD = ");
Serial.println(sval);
LightLEDs(0,100,0); //540 lights
delay (1000);
Analogpin = (Analogpin + 1);
sens = ReadSensor();
Serial.print("538 PD = ");
Serial.println(sval);
LightLEDs(0,0,100); //600 lights
delay (1000);
Analogpin = (Analogpin + 1);
sens = ReadSensor();
Serial.print("600 PD = ");
Serial.println(sval);
}
void LightLEDs(int blue, int green, int red) {
analogWrite(LED476, blue); analogWrite(LED538, green); analogWrite(LED600, red);
}
int ReadSensor() {
int i =0;
for (i = 0; i < 3; i++){
sval = analogRead(Analogpin); // read PD sensors (on analog pins 1,2 and 3 total);
/*Serial.print("first readsensor sval = "); //debugging prints
Serial.println(sval);
Serial.print(" and Analogpin = ");
Serial.println(Analogpin);
*/
sval = map(sval, 0, 1023, 0, 255);
return sval;
}
}
I have read all of the 2 arduino board I2C tutorials that I could google including Mr. Gammon's. I used 2 meters of good quality signal wire to connect the SCL, SDA and GND pins together (Master is Uno R3 and Slave is Leonardo). I loaded Master/Slave examples and they worked just fine.
If you're still with me, my problem is trying to work out where to plug the Wire.write and other instructions in this existing sketch.
The Loop sequence first calls function LightLEDs and passes the PWM factors. I use PWM because I want to be able to change the incident light intensity to suit a given dye concentration. The readings will be quantified by comparison to standards
The LED burns for a full second to ensure it has "settled down". Does that seem sufficient time? In practice, using standard the readings are very consistent so I think 1 second is good.
Then the ReadSensor routine maps the analog values to 0 to 255.
Could you advise how to handle sending this data, the 3 readings to Master? I first thought to send after each reading. Now I think it would be best to save the 3 reading in an array and then send them as a group. I wonder if I need another function just for the Wire instructions.
I seem to be chasing my tail. It seems I2c likes characters so I've spent a lot of time trying to convert integers into char with the idea to revert and then back again with the Master. But others send integers via I2C... I need some guidance as to the right way to handle this task, please.
Perhaps a few lines of code to get me started would be very helpful too.
