From ef69f7a8f7bfe888cfa53a93a26dc57c036568a9 Mon Sep 17 00:00:00 2001 From: Philip Lampkin Date: Tue, 29 Dec 2020 21:11:36 -0600 Subject: Commented and added blink LED function on receive data --- .../Slave_Random_LED_I2C/Slave_Random_LED_I2C.ino | 102 +++++++++++---------- 1 file changed, 52 insertions(+), 50 deletions(-) diff --git a/Digital Photoreactor Controller Firmware/Test Sketchs/Slave_Random_LED_I2C/Slave_Random_LED_I2C.ino b/Digital Photoreactor Controller Firmware/Test Sketchs/Slave_Random_LED_I2C/Slave_Random_LED_I2C.ino index 7f7a7f6..ef9df86 100644 --- a/Digital Photoreactor Controller Firmware/Test Sketchs/Slave_Random_LED_I2C/Slave_Random_LED_I2C.ino +++ b/Digital Photoreactor Controller Firmware/Test Sketchs/Slave_Random_LED_I2C/Slave_Random_LED_I2C.ino @@ -1,47 +1,44 @@ - -// --------------------------------- -// Stress test program/example for TinyWireS I2C library. -// Run this slave program on the Attiny. -// Run the other master program on the Arduino Uno R3. -// --------------------------------- -// // Written by Scott Hartog, 2/6/2016, to stress test the TinyWireS library. -// https://github.com/rambo/TinyWire -// -// This project uses the Tiny85 as an I2C slave. -// -// The slave program using TinyWireS, running on a Attiny85, receives -// N bytes of random data in a single receiveEvent() callback and -// stores that data in a global buffer. It then responds the first requestEvent() -// callback with that same data. The requestEvent() callback overwrites the data -// buffer with zeros after responding so it will only respond correctly to the -// first requestEvent() callback after each receiveEvent() callback. Subsequent -// requestEvent() will respond with 0xff for all data bytes. -// -// -// SETUP: -// AtTiny Pin 5 (PB0/SDA) = I2C SDA -// connect to SDA on master with external pull-up (~4.7K) -// AtTiny Pin 7 (PB0/SCL) = I2C SCL -// connect to SCL on master with external pull-up (~4.7K) -// AtTiny Pin 1 (PB5/!RST) -// connect to reset on master (or just pull-up) -// -// Please see credits and usage for usiTwiSlave and TinyWireS in the .h files of -// those libraries. +//////////////////////////////////////////////////////////////////////////////////// +// I2C test program for Digital Photoreactor Controller using TinyWireS library // +// Run this slave program on the AtTiny85. // +//////////////////////////////////////////////////////////////////////////////////// +// This test program is adapted from the TinyWireS stress test program // +// developed by Scott Hartog: https://github.com/rambo/TinyWire // +// // +// The slave program receives via I2C N bytes of random data in a single // +// receiveEvent() callback and stores that data in a global buffer. // +// It then responds the first requestEvent() callback with the same data. // +// The requestEvent() callback overwrites the data buffer with zeros after // +// responding so it will only respond correctly to the first requestEvent() // +// callback after each receiveEvent() callback. Subsequent requestEvent() // +// will respond with 0xff for all data bytes. // +// // +// // +// SETUP: // +// AtTiny85 Pin 2 = PB3 = Blink LED // +// Attiny85 Pin 3 (PB4) = LED PWM // +// AtTiny85 Pin 5 (PB0/SDA) = I2C SDA // +// connect to SDA on master with external pull-up (~4.7K) // +// AtTinyPin 6 (PB1) = Fan PWM // +// AtTiny Pin 7 (PB0/SCL) = I2C SCL // +// connect to SCL on master with external pull-up (~4.7K) // +// AtTiny Pin 1 (PB5/!RST) // +// connect to reset on master (or just pull-up) // +// // +//////////////////////////////////////////////////////////////////////////////////// #include "TinyWireS.h" // wrapper class for I2C slave routines -#define I2C_SLAVE_ADDR 0x26 // i2c slave address (38, 0x26) +#define I2C_SLAVE_ADDR 0x26 // I2C slave address (38, 0x26), -// global buffer to store data sent from the master. -uint8_t master_data[16]; -// global variable to number of bytes sent from the master. -uint8_t master_bytes; +uint8_t master_data[16]; // global buffer to store data sent from the master. +uint8_t master_bytes; // global variable to number of bytes sent from the master. -const int LEDPin = 4; //pin 4 arduino, pin 3 on ATTiny85) -const int FanPin = 1; // pin 1 arduino, pin 6 on ATtiny85) +const int LEDPin = 4; //Arduino pin 4, pin 3 on AtTiny85 pinout +const int FanPin = 1; //Arduino pin 1, pin 6 on AtTiny85 pinout +const int BlinkPin = 3; //Arduino pin 3, pin 2 on AtTiny85 pinout -// Gets called when the ATtiny receives an i2c write slave request +// Gets called when the ATtiny85 receives an I2C write slave request void receiveEvent(uint8_t num_bytes) { uint8_t i; @@ -52,9 +49,16 @@ void receiveEvent(uint8_t num_bytes) // store the data from the master into the data buffer for (i = 0; i < master_bytes; i++) master_data[i] = TinyWireS.receive(); - - analogWrite(FanPin, master_data[0]); - analogWrite(LEDPin, master_data[1]); + + // set LED and Fan PWM duty to random byte values recieved from the master + analogWrite(FanPin, master_data[0]); + analogWrite(LEDPin, master_data[1]); + + // blink LED whenever data is successfuly recieved + digitalWrite(BlinkPin, HIGH); + delay(10); + digitalWrite(BlinkPin, LOW); + delay(10); } @@ -102,19 +106,17 @@ void setup() // register the onRequest() callback function TinyWireS.onRequest(requestEvent); - pinMode(LEDPin, OUTPUT); - pinMode(FanPin, OUTPUT); + pinMode(LEDPin, OUTPUT); // set pin for LED PWM + pinMode(FanPin, OUTPUT); // set pin for Fan PWM + pinMode(BlinkPin, OUTPUT); // set pin to blink LED - TCCR0B = TCCR0B & 0b11111000 | 0b001; // Sets internal timer to 31250 Hz, and PWM frequency on PB0 (Arduino pin 4) to 31250 Hz, fast enough for a fan! - - //analogWrite(FanPin, FanPwm); - //analogWrite(LEDPin, LEDPwm); - + TCCR0B = TCCR0B & 0b11111000 | 0b001; // sets internal timer of AtTiny85 to 31250 Hz for fast PWM for controlling fan speed } void loop() { - // This needs to be here - TinyWireS_stop_check(); + // this needs to be here + /////////////Does this really need to be here????? + //TinyWireS_stop_check(); // otherwise empty loop } -- cgit v1.2.3