Forums › Life › Computers, Gadgets & Technology › arduino sketch for studio control system
Ì’ve put this here as its easier to look at it when I am at my office than opening a SSH tunnel to my NAS – and it was too long for the blog also it works but has a minor bug (it will only start correctly and put the LEDs on after a reset, when first powerd up the whole LED stack is blanco in spite of me trying to initialise and update them in setup{}
it is 25 years since I was last coding, I probably have done something very simple and silly, maybe leveret or someone with more brain than me can spot it (I already notice that I’ve got a few “off by one” issues in the debug notices as the array of course starts at 00 rather than 01)
// 2013-11-05 updated
// new debounce library added
// now used for door contact
// button handling vars
// 01 (pin 13) -green (muted)
// 02-(pin 11) red LED
// 03-(pin 10) watchdog LED
// 04-(pin 7) trigger LED
// 2013-11-23 – added control for relays
const int buttonPins[4] = {2,4,8,9};
const int ledPins[4] = {13,11,10,7};
const int reedPin= 12 ; // reed switch
const int relay1Pin = 6 ;
const int relay2Pin = 5 ;
// this ia LDR brightness if telephone strobe is on
const int strobeVal = 700 ;
const int btnDelay = 20 ; // button deley
const int reedDelay= 25 ; //
// define the constants for the remote code
// and timing delays
#define MUTE 0x140C
#define VOLUME_UP 0x240C
#define VOLUME_DOWN 0x640C
#define IR_delay1 5
#define IR_delay2 5
#define REPEATS 2
#define REED_delay 50
#include
#include <Bounce.h>
// global declarations & vars
IRsend irsend;
Bounce btn01 = Bounce (buttonPins[0],btnDelay);
Bounce btn02 = Bounce (buttonPins[1],btnDelay);
Bounce btn03 = Bounce (buttonPins[2],btnDelay);
Bounce btn04 = Bounce (buttonPins[3],btnDelay);
Bounce reed = Bounce(reedPin, reedDelay );
int incomingbyte = 0;
int ledState[4] = { HIGH, LOW, LOW, LOW};
int btnChange[4] = { LOW, LOW, LOW, LOW };
int reedChange = LOW ;
int btnState[4] = { HIGH, HIGH, HIGH, HIGH}; // as these are pulled up (active low)
int reedState=HIGH ;
int reedTime= 0 ;
int iptChange = LOW ;
int trigLedLatch = LOW ;
int muteState = LOW ;
int relay2State = LOW ;
int ldrValue = LOW ;
long previousMillis = 0;
long lastMuteTime = 0 ;
long trigLedTime = 0 ;
long barkInterval = 375 ; // bark the watchdog LED every 375 ms
long trigInterval = 375 ; // flash command received LED
void setup()
{
Serial.begin(9600);
pinMode(buttonPins[0], INPUT);
pinMode(buttonPins[1], INPUT);
pinMode(buttonPins[2], INPUT);
pinMode(buttonPins[3], INPUT);
pinMode(ledPins[0], OUTPUT);
pinMode(ledPins[1], OUTPUT);
pinMode(ledPins[2], OUTPUT);
pinMode(ledPins[3], OUTPUT);
pinMode(relay1Pin, OUTPUT);
pinMode(relay2Pin, OUTPUT);
for (int i = 0 ; i < 4 ; i++)
{
Serial.print(i);
Serial.print(“:”);
Serial.print(“IN:”);
Serial.print(buttonPins);
Serial.print(“UIT:”);
Serial.print(ledPins);
Serial.print(“*”);
}
Serial.println(“”);
ledState[3] = HIGH ; // watchdog
// and initialise sensors
btnChange[0] =btn01.update();
btnChange[1] =btn02.update();
btnChange[2] =btn03.update();
btnChange[3] =btn04.update();
reedChange =reed.update();
iptChange = HIGH ; // will this make the sketch
// start correct on first run?
btnState[0] = !(btn01.read());
btnState[1] = !(btn02.read());
btnState[2] = !(btn03.read());
btnState[3] = !(btn04.read());
reedState = reed.read(); // 🙂
reedTime = reed.duration();
// initialise leds
for (int i = 0 ; i < 4 ; i++)
{
digitalWrite(ledPins[1],ledState);
}
}
void loop()
{
btnChange[0] =btn01.update();
btnChange[1] =btn02.update();
btnChange[2] =btn03.update();
btnChange[3] =btn04.update();
reedChange =reed.update();
iptChange = LOW ;
btnState[0] = !(btn01.read());
btnState[1] = !(btn02.read());
btnState[2] = !(btn03.read());
btnState[3] = !(btn04.read());
reedState = reed.read(); // 🙂
reedTime = reed.duration();
// get LDRvalue
ldrValue = analogRead (A0) ;
// green button
if ( btnChange[0] && btnState[0] ) {
// ledState[2] = !ledState[2] ;
ledState[0] = HIGH ;
ledState[1] = LOW ;
iptChange = HIGH ;
Serial.println(“btn 00 state change – green light mute off”);
muteState = LOW ;
}
// red button
if ( btnChange[1] && btnState[1] ) {
// ledState[2] = !ledState[2] ;
ledState[0] = LOW ;
ledState[1] = HIGH ;
Serial.println(“btn 01 state change – red light mute ON”);
Serial.println(“sending MUTE to IR led”);
iptChange = HIGH ;
// irEmit(MUTE,REPEATS);
muteState = HIGH ;
}
if ( btnChange[2] && btnState[2]) {
// ledState[2] = !ledState[2] ;
Serial.println(“btn3 state change”);
Serial.println(“sending VOL dOWN to IR led”);
iptChange = HIGH ;
irEmit(VOLUME_DOWN,REPEATS);
}
if ( btnChange[3] && btnState[3]) {
// ledState[2] = !ledState[2] ;
Serial.println(“btn4 state change”);
Serial.println(“sending VOL UP to IR led”);
iptChange = HIGH ;
irEmit(VOLUME_UP,REPEATS);
}
// check reae, beware as this will be high when the door is open
if ( reedChange ) {
iptChange = HIGH ;
if ( reedState) {
ledState[0] = LOW ;
ledState[1] = HIGH ;
muteState = HIGH ;
Serial.println(“door OPEN – red/MUTE”);
// Serial.println(“sending MUTE to IR led”);
//
// irEmit(MUTE,REPEATS);
}
if ( !reedState) {
ledState[0] = HIGH ;
ledState[1] = LOW ;
muteState = LOW ;
Serial.println(“door CLOSED – red/sound through”);
}
}
// set the relay
digitalWrite (relay1Pin, muteState );
barkInterval = 375 ;
if ( ldrValue > strobeVal ) {
// ledState[2] = HIGH ;
iptChange = HIGH ;
barkInterval = 50 ;
Serial.print(“A0 val:”);
Serial.println(ldrValue);
}
// bark the watchdog LED
unsigned long currentMillis = millis();
if (currentMillis – previousMillis > barkInterval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState[3] = !ledState[3] ;
}
// check for input changes and send them to serialport
// latch the trigger LED on for enough time (as the actual input change is
// only a few ms
//
if ( iptChange) {
trigLedLatch = HIGH ;
ledState[2] = HIGH ;
trigLedTime = millis() ;
Serial.print(“L01:”);
Serial.print(ledState[0]);
Serial.print(“t”);
Serial.print(“L02:”);
Serial.print(ledState[1]);
Serial.print(“t”);
Serial.print(“L03:”);
Serial.print(ledState[2]);
Serial.print(“t”);
Serial.print(“L04:”);
Serial.print(ledState[3]);
Serial.print(“n”);
}
if ((currentMillis – trigLedTime) > trigInterval ) {
ledState[2] = LOW ;
trigLedLatch = LOW ;
}
// update leds
ledEmit(4);
}
// Functions now go here at end of sketch
void irEmit(int code, int repeats)
{
for (int i = 0 ; i < (repeats) ; i++)
{
irsend.sendSony(code, 15);
delay(IR_delay1);
irsend.sendSony(code, 15);
delay(IR_delay1);
irsend.sendSony(code, 15);
delay(IR_delay1);
irsend.sendSony(code, 15);
delay(IR_delay2);
}
}
void ledEmit(int maxLeds)
{
for (int i = 0 ; i < (maxLeds) ; i++)
{
digitalWrite(ledPins,ledState);
}
}
D’oh!
here’s one for a start, and really a silly schoolboy error
for (int i = 0 ; i < 4 ; i++)
{
digitalWrite(ledPinsB][COLOR=”#FF0000″]1[/COLOR][/B,ledStateB][COLOR=”#FF0000″]i[/COLOR][/B);
}
but that still doesn’t explain why the watchdog LED doesn’t work until the sketch is reset, as normally setup{} is followed by loop{} immediately, and surely it would trigger off 375ms after the sketch has run?
0
Voices
0
Replies
Tags
This topic has no tags
Forums › Life › Computers, Gadgets & Technology › arduino sketch for studio control system