Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Arduino IDE demo source code for KC868-A8]--#08-Serial control relays
#1
Star 
Code 7: //The demo code is SERIAL-CONTROL-RELAY    You can copy the code to your Arduino IDE

Code:
#include "Arduino.h"
#include "PCF8574.h"
PCF8574 pcf8574(0x24);
char cmd='0';
void setup(){
  pcf8574.pinMode(P0, OUTPUT);
  pcf8574.pinMode(P1, OUTPUT);
  pcf8574.pinMode(P2, OUTPUT);
  pcf8574.pinMode(P3, OUTPUT);
  pcf8574.pinMode(P4, OUTPUT);
  pcf8574.pinMode(P5, OUTPUT);
  pcf8574.pinMode(P6, OUTPUT);
  pcf8574.pinMode(P7, OUTPUT);

  Serial.begin(9600);
    Serial.print("Init pcf8574...");
  if (pcf8574.begin()){
    Serial.println("OK");
  }else{
    Serial.println("KO");

  pcf8574.digitalWrite(P0, HIGH);
  pcf8574.digitalWrite(P1, HIGH);
  pcf8574.digitalWrite(P2, HIGH);
  pcf8574.digitalWrite(P3, HIGH);
  pcf8574.digitalWrite(P4, HIGH);
  pcf8574.digitalWrite(P5, HIGH);
  pcf8574.digitalWrite(P6, HIGH);
  pcf8574.digitalWrite(P7, HIGH);
  }
}

void loop(){
  while(Serial.available())  // To Get up ALL Serial data
  {
    cmd=Serial.read();
   
  }

    if (cmd=='1')
    {
      pcf8574.digitalWrite(P0, LOW);
      Serial.println("You've turned on the Relay");
      cmd = '0';
    }
    else if(cmd == '2')
    {
      pcf8574.digitalWrite(P0, HIGH);
      Serial.println("You've turned off the Relay");
      cmd = '0';
    }
}
   
Reply


Forum Jump:


Users browsing this thread:
1 Guest(s)