ericek111

Arduino Relays

Feb 17th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.77 KB | None | 0 0
  1. /*********************************************************************
  2.   This is an example for our Monochrome OLEDs based on SSD1306 drivers
  3.  
  4.   Pick one up today in the adafruit shop!
  5.   ------> http://www.adafruit.com/category/63_98
  6.  
  7.   This example is for a 128x64 size display using I2C to communicate
  8.   3 pins are required to interface (2 I2C and one reset)
  9.  
  10.   Adafruit invests time and resources providing this open source code,
  11.   please support Adafruit and open-source hardware by purchasing
  12.   products from Adafruit!
  13.  
  14.   Written by Limor Fried/Ladyada  for Adafruit Industries.
  15.   BSD license, check license.txt for more information
  16.   All text above, and the splash screen must be included in any redistribution
  17. *********************************************************************/
  18.  
  19. #include <SPI.h>
  20. #include <Wire.h>
  21. #include <Adafruit_GFX.h>
  22. #include <Adafruit_SSD1306.h>
  23.  
  24. #define OLED_RESET 4
  25. Adafruit_SSD1306 display(OLED_RESET);
  26.  
  27. #if (SSD1306_LCDHEIGHT != 64)
  28. #error("Height incorrect, please fix Adafruit_SSD1306.h!");
  29. #endif
  30. #define RELAY1 4
  31. #define RELAY2 5
  32. #define RELAY3 6
  33. #define RELAY4 7
  34. #define DELAY 1000
  35.  
  36. void setup()   {
  37.   Serial.begin(9600);
  38.   pinMode(RELAY1, OUTPUT);
  39.   pinMode(RELAY2, OUTPUT);
  40.   pinMode(RELAY3, OUTPUT);
  41.   pinMode(RELAY4, OUTPUT);
  42.  
  43.   // by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
  44.   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  // initialize with the I2C addr 0x3D (for the 128x64)
  45.   // init done
  46.  
  47.     display.clearDisplay();
  48.   display.setTextSize(2);
  49.   display.setTextColor(WHITE);
  50.   display.setCursor(0, 0);
  51.   display.setCursor(30, 0);
  52.   display.println("Relays");
  53.   display.setCursor(0, 20);
  54.   display.println("1 2 3 4");
  55.   display.display();
  56.   switchRelay(RELAY1, 0, false);
  57.   switchRelay(RELAY2, 1, false);
  58.   switchRelay(RELAY3, 2, false);
  59.   switchRelay(RELAY4, 3, false);
  60. }
  61.  
  62.  
  63. void loop() {
  64.    // 1 = OFF; 0 = ON
  65.  
  66.    // Relay 1 ON
  67.    switchRelay(RELAY1, 0, true);
  68.    delay(DELAY);
  69.  
  70.    // Relay 2 ON
  71.    switchRelay(RELAY2, 1, true);
  72.    delay(DELAY);
  73.    
  74.    // Relay 3 ON
  75.    switchRelay(RELAY3, 2, true);
  76.    delay(DELAY);
  77.    
  78.    // Relay 4 ON
  79.    switchRelay(RELAY4, 3, true);
  80.    delay(DELAY);
  81.    
  82.    // Relay 1 OFF
  83.    switchRelay(RELAY1, 0, false);
  84.    delay(DELAY);
  85.  
  86.    // Relay 2 OFF
  87.    switchRelay(RELAY2, 1, false);
  88.    delay(DELAY);
  89.    
  90.    // Relay 3 OFF
  91.    switchRelay(RELAY3, 2, false);
  92.    delay(DELAY);
  93.    
  94.    // Relay 4 OFF
  95.    switchRelay(RELAY4, 3, false);
  96.    delay(DELAY);
  97. }
  98.  
  99. void switchRelay(int port, int order, bool state) {
  100.   digitalWrite(port, state ? 0 : 1);
  101.   display.fillRect(order*24, 35, 15, 15, (uint8_t)0);
  102.   display.setTextSize(2);
  103.   display.setCursor(order*24, 35);
  104.   display.print(state ? "I " : "O ");
  105.   display.display();
  106. }
Advertisement
Add Comment
Please, Sign In to add comment