<commit_before>'use strict';

/*
- Relay, active low
Pins should be digital out
in1, pin7, gpio 4
in2, pin9, gpio 17
in3, pin11, gpio 27
in4, pin 13, gpio 22

- Led, active high (digital out)
pin 16, gpio 23

- Button,  pulled high, active low (digital in)
pin 40, gpio 21

- Button, pulled low, active high (digital in)
pin 38, gpio 20

- Potentiometer (analog in)
pin 12, gpio 18

- Motion sensor (digital in)
pin 37, gpio 26 
*/

const Cleanup = require('./cleanup');
const gpio = require('rpi-gpio');

const pinButtonActiveHigh = 21;
const pinButtonActiveLow = 20;
const pinLed = 23;
const pinMotion = 26;

const blinkLed = (times, delay) => {
    let counter = 0;
    const blink = () => {
        counter++;
        gpio.write(pinLed, true, (err) => {
            if (err) {
                console.log('Error blinking led..');
            } else {
                global.setTimeout(() => {
                    gpio.write(pinLed, false);
                    if (counter < times) global.setTimeout(blink, delay);
                }, delay);
            }
        });
    }
}

gpio.setup(pinButtonActiveHigh, gpio.DIR_IN, () => {
    gpio.read(pinButtonActiveHigh, function(err, value) {
        console.log('Button, active HIGH value is: ' + value);
    });
});
gpio.setup(pinButtonActiveLow, gpio.DIR_IN, () => {
    gpio.read(pinButtonActiveLow, function(err, value) {
        console.log('Button, active LOW value is: ' + value);
    });
});
gpio.setup(pinLed,  gpio.DIR_OUT, () => {
    gpio.write(pinLed, false, (err) => {
        console.log('Initialized led to off...');
    })
});

gpio.on('change', function(channel, value) {
    console.log('Channel ' + channel + ' value is now ' + value);
    if (channel === pinButtonActiveLow) {
        blinkLed(2, 200);
    } else if (channel === pinButtonActiveHigh) {
        blinkLed(1, 200);
    }
});
gpio.setup(pinButtonActiveHigh, gpio.DIR_IN, gpio.EDGE_BOTH);
gpio.setup(pinButtonActiveLow, gpio.DIR_IN, gpio.EDGE_BOTH);

// setup termination listener
Cleanup((gpio) => {
    gpio.destroy();<commit_msg>Updated correct pin numbering and made everything work okay<commit_after>'use strict';

/*
- Relay, active low
Pins should be digital out
in1, pin7, gpio 4
in2, pin9, gpio 17
in3, pin11, gpio 27
in4, pin 13, gpio 22

- Led, active high (digital out)
pin 16, gpio 23

- Button,  pulled high, active low (digital in)
pin 40, gpio 21

- Button, pulled low, active high (digital in)
pin 38, gpio 20

- Potentiometer (analog in)
pin 12, gpio 18

- Motion sensor (digital in)
pin 37, gpio 26 
*/

const Cleanup = require('./cleanup');
const gpio = require('rpi-gpio');

gpio.setMode(gpio.MODE_RPI);

const pinButtonActiveHigh = 40;
const pinButtonActiveLow = 38;
const pinLed = 16;
const pinMotion = 37;
const pinRelay1 = 7;
const pinRelay2 = 11;
const pinRelay3 = 13;
const pinRelay4 = 15;
const pinRelays = [pinRelay1, pinRelay2, pinRelay3, pinRelay4];

const blinkLed = (times, delay) => {
    let counter = 0;
    const blink = () => {
        counter++;
        gpio.write(pinLed, true, (err) => {
            if (err) {
                console.log('Error blinking led..');
            } else {
                global.setTimeout(() => {
                    gpio.write(pinLed, false);
                    if (counter < times) global.setTimeout(blink, delay);
                }, delay);
            }
        });
    }
    blink();
}

gpio.setup(pinLed,  gpio.DIR_OUT, () => {
    console.log('Initialized led...');
    gpio.write(pinLed, false, (err) => {
        console.log('Initialized led to off...');
    })
});
pinRelays.forEach(pin => {
        gpio.setup(pin, gpio.DIR_OUT, () => {
                gpio.write(pin, true);
        });
});

gpio.on('change', function(channel, value) {
    if (channel === pinButtonActiveLow) {
        console.log("button active low pressed");
        blinkLed(2, 200);
    } else if (channel === pinButtonActiveHigh) {
        console.log("button active high pressed");
        blinkLed(1, 200);
    } else if (channel === pinMotion) {
        console.log("motion...");
        blinkLed(5, 100);
    }
});
gpio.setup(pinButtonActiveHigh, gpio.DIR_IN, gpio.EDGE_RISING);
gpio.setup(pinButtonActiveLow, gpio.DIR_IN, gpio.EDGE_FALLING);
gpio.setup(pinMotion, gpio.DIR_IN, gpio.EDGE_FALLING);


let state = true;
const toggle = () => {
    state = !state;
    pinRelays.forEach(pin => {
        gpio.write(pin, state);
    });
    delayed();
}
const delayed = () => {
    global.setTimeout(toggle, 2000);
}
delayed();


// setup termination listener
Cleanup(() => {
    gpio.destroy();
{'fraction_non_alphanumeric': 0.1488108226052804, 'fraction_numerical': 0.028583897010691688, 'mean_word_length': 2.6642685851318944, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1098968', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1534, 'n_words': 402}