Party Vibe

Register

Welcome To

arduino sketch for studio control system

Forums Life Computers, Gadgets & Technology arduino sketch for studio control system

  • This topic is empty.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • Ì’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)

    Quote:
    /* Control a sony receiver via Arduino and IR
    / auto mute when door is open
    / using reed swich on door
    / 2011/2012/2013 Alex@The Rats Nest, Ipswich, UK
    / IR code taken from Ken Shirriff’s blog: A Multi-Protocol Infrared Remote Library for the Arduino
    / and remote codes from here http://lirc.sourceforge.net/remotes/sony/RM-AAU014
    /
    /
    / also 4 x buttons, for vol up/down, toggle green/red led (as they can get out of sync)
    / and force mute
    / NB: This receiver uses 15 bit protocol
    / and only needs 50ms delay after each command sent twice
    / per button press
    */

    // 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

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.

Forums Life Computers, Gadgets & Technology arduino sketch for studio control system