Back to Parent

Code
int redLedPin = D0;
int greenLedPin = D1;
int blueLedPin = D2;

int redButtonPin = D3;
int greenButtonPin = D4;
int blueButtonPin = D5;

int switchPowerPin = D7;
int switchPin = D8;

char *send_arr;
char *receive_arr;
int send_arr_count;


#define ARR_LENGTH 9

void setup() {
    pinMode(redLedPin, OUTPUT);
    pinMode(greenLedPin, OUTPUT);
    pinMode(blueLedPin, OUTPUT);
    pinMode(switchPowerPin, OUTPUT);
    
    digitalWrite(switchPowerPin, HIGH);
    
    pinMode(redButtonPin, INPUT_PULLUP);
    pinMode(greenButtonPin, INPUT_PULLUP);
    pinMode(blueButtonPin, INPUT_PULLUP);
    pinMode(switchPin, INPUT_PULLDOWN);
    
    
    Time.zone(-4);//set to est

    send_arr = (char*) calloc(ARR_LENGTH+1, sizeof(char));
    receive_arr = (char*) calloc(ARR_LENGTH+1, sizeof(char));
    send_arr_count = 0;
    
    setupReceiver();
    
    Serial.begin(9600);
}

void setupReceiver() {
    Particle.subscribe("button-presses", buttonPresses, MY_DEVICES);
}

void loop() {
    if (isButtonPressed(switchPin)) {
        loopReceiver();
    } else {
        loopSender();
    }
}

void loopReceiver() {
   send_arr_count = 0;
   char but = 0;
   int on_time = 200;
   int off_time = 100;
   
   delay(1000);
   for(int i = 0; i < ARR_LENGTH; i++) {
        if (receive_arr[i] == '0') {
            digitalWrite(redLedPin, HIGH);
            delay(on_time);
            digitalWrite(redLedPin, LOW);
            delay(off_time);
        } else if (receive_arr[i] == '1') {
            digitalWrite(greenLedPin, HIGH);
            delay(on_time);
            digitalWrite(greenLedPin, LOW);
            delay(off_time);
        } if (receive_arr[i] == '2') {
            digitalWrite(blueLedPin, HIGH);
            delay(on_time);
            digitalWrite(blueLedPin, LOW);
            delay(off_time);
        }
   }
}

void buttonPresses(const char *event, const char *data) {
    for(int i =0; i < ARR_LENGTH; i++) {
        receive_arr[i] = data[i];//copy into our array
    }
}

bool isButtonPressed(int pin) {
    int digitalValue = digitalRead(pin);
    //Serial.printf("Pin %d has value %d\n", pin, digitalValue);
    return digitalValue == LOW;
}

void loopSender() {
    if(isButtonPressed(redButtonPin)) {
        digitalWriteFast(redLedPin, HIGH);
        if (send_arr_count == 0 || send_arr[send_arr_count-1] != '0') {
            send_arr[send_arr_count++] = '0';
        }
        //digitalWrite(greenLedPin, LOW);
        //digitalWrite(blueLedPin, LOW);
    } else if(isButtonPressed(greenButtonPin)) {
        //digitalWrite(redLedPin, LOW);
        if (send_arr_count == 0 || send_arr[send_arr_count-1] != '1') {
            send_arr[send_arr_count++] = '1';
        }
        digitalWriteFast(greenLedPin, HIGH);
        //digitalWrite(blueLedPin, LOW);
    } else if(isButtonPressed(blueButtonPin)) {
        //digitalWrite(redLedPin, LOW);
        if (send_arr_count == 0 || send_arr[send_arr_count-1] != '2') {
            send_arr[send_arr_count++] = '2';
        }
        //digitalWrite(greenLedPin, LOW);
        digitalWriteFast(blueLedPin, HIGH);
    } else {
        digitalWriteFast(redLedPin, LOW);
        digitalWriteFast(greenLedPin, LOW);
        digitalWriteFast(blueLedPin, LOW);
    }

   if(send_arr_count == ARR_LENGTH) {
       send_arr[ARR_LENGTH] = '\0';
       Particle.publish("button-presses", send_arr, PRIVATE);
       send_arr_count = 0;
   }
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0