Commit 20985e21 authored by Philip Hozier's avatar Philip Hozier
Browse files

wk13 sketches 5/5

parent 06c83b4a
Pipeline #228928 passed with stages
in 13 seconds
/////////////////////////////ATyiny1614
//define pins
////// I2C
#define SCL_pin B0
#define SDA_pin B1
////// SERIAL
// #define Tx_pin B2
// #define Rx_pin B3
////// UPDI
// #define UPDI_pin A0
////// SPI
#define MOSI_pin A1
#define MISO_pin A2
#define SCK_pin A3
#define CS_pin A4
////// OUTPUTS
#define LED1_pin A5
#define LED2_pin A6
/////// INPUT
#define BTN_pin A7
//////////////////////////////
///////// INCLUDES ///////////
#include <Wire.h>
///////////// VARIABLES //////
int baudRate = 9600;
int broadcastDelay = 500;
byte Modus = 1;
int totalModes = 2;
//////////////////////////////
void setup() {
// put your setup code here, to run once:
// INPUT
pinMode(BTN_pin, INPUT_PULLUP);
// OUPUTS
pinMode(LED1_pin, OUTPUT);
pinMode(LED2_pin, OUTPUT);
// Serial - FTDI
Serial.begin(baudRate);
while (!Serial);
//// I2C COMMS
Wire.begin(); // join i2c bus (address optional for master)
}
void loop() {
// put your main code here, to run repeatedly:
if (digitalRead(BTN_pin) == LOW) {
Broadcast(1, Modus); // send Node 1 a number
Broadcast(2, Modus); // send Node 2 a number
if(Modus>=totalModes){
Modus= 0; //reset;
}else{
Modus++;
}
delay(broadcastDelay);
}
}
/////////////// Broadcast /////////
void Broadcast(int whichAddress, byte myMessage) {
Serial.println("------------------BROADCAST START");
Serial.print(whichAddress);
Serial.print(",");
Serial.println(myMessage);
////
Wire.beginTransmission(whichAddress); // transmit to device #4
Blink(LED1_pin); // controller feedback
Wire.write(myMessage); // sends bytes
Wire.endTransmission(); // stop transmitting
Blink(LED2_pin); // controller feedback
Serial.println("------------------BROADCAST END");
}
/////////////////////////////ATtiny412
//define pins
////// I2C
#define SDA_pin A1 // pin 4
#define SCL_pin A2 // pin 5
////// SERIAL
#define Tx_pin B2
#define Rx_pin B3
////// OUTPUTS
#define LED_Rgb A6 // pin2
#define LED_rGb A7 // pin3
#define LED_rgB A3 // pin7
////// Variables
# define baud_Rate 9600
# define myAddress 1 // add address here
// RGB ARRAYS
int RGB_Array[] = {LED_Rgb, LED_rGb, LED_rgB};
int RGB_ArrayCount = sizeof(RGB_Array) / sizeof(RGB_Array[0]);
// OTHER variables
int myDelay = 100;
//////////////////////////////
//// INCLUDES
#include <Wire.h>
//////////////////////////////
void setup() {
// put your setup code here, to run once:
// OUPUTS
pinMode(LED_Rgb, OUTPUT);
pinMode(LED_rGb, OUTPUT);
pinMode(LED_rgB, OUTPUT);
// COMMS
RGB_off();// INITIAL STATE
Wire.begin(myAddress); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
// Serial
Serial.swap(1);// solves single RGB red led problem
Serial.begin(baud_Rate); // start serial for output
while (!Serial);
}
void loop() {
// put your main code here, to run repeatedly:
delay(myDelay);
}
///////////////////////////////////////
void receiveEvent(int howMany) {
int x = Wire.read(); // receive byte as an integer
switch (x) {
case 0:
// OFF
RGB_off();
break;
case 1:
// ON
RGB_on();
break;
default:
RGB_off();
break;
}
}
////////////////////
void RGB_off() {
for (int k = 0; k <= RGB_ArrayCount - 1; k++) {
digitalWrite(RGB_Array[k], HIGH);
}
}
///////////////
void RGB_on() {
// seperately on/off
for (int k = 0; k <= RGB_ArrayCount - 1; k++) {
digitalWrite(RGB_Array[k], LOW);
}
}
///////////////////
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment