Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EEPROM.h>
- #include <Stepper.h>
- #include<Timer1.h>
- #define digit1 5 // Segment Digit 1
- #define digit2 6 // Segment Digit 2
- #define RECV_PIN 7 // IR Receiver
- #define relay 8 // RelayPin
- #define buzzer 13
- #define clock A4 // Segment Clock
- #define data A5 // Segment Data
- #define MotorPin A0 // Triac Pin
- //----------------------------------------------------------------------------------------------//
- // Stepper motor
- unsigned long currentMillis;
- unsigned long previousMillis = 0;
- const long SwingInterval = 1000;
- const float STEPS_PER_REV = 16; //default 32
- const float GEAR_RED = 32; //default 64
- //const float STEPS_PER_OUT_REV = STEPS_PER_REV * GEAR_RED;
- const float STEPS_PER_OUT_REV = 1024;
- int StepsRequired;
- Stepper steppermotor(STEPS_PER_REV, 9, 11, 10, 12);
- int swingState = LOW; // swing close by default
- byte PreviousSwingState; // EEPROM VAlues
- //----------------------------------------------------------------------------------------------//
- // Segment Display
- byte Digit[] = {B00010010, B11011110, B00010101, B10010100, // 0 1 2 3
- B11011000, B10110000, B00110000, B11010110, // 4 5 6 7
- B00010000, B11010000, B01110001, B00111011, // 8 9 F L
- B10110000, B00000000, B11111111, B01111100
- }; // S Ao Af n
- // 0 1 2 3 4 5 6 7 8 9 F L S Ao Af n
- // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
- //byte symbol[] = {B00000001, B11111110};
- int unit, tens;
- volatile char digNum = 1;
- //----------------------------------------------------------------------------------------------//
- // Motor Speed Control
- //int fanval[5] = {75, 45, 10}; // Fan different Speed
- int fanval[5] = {75, 45, 5}; // Fan different Speed
- int timeval;
- bool interruptState = false;
- boolean fan;
- byte speedCounter = 0;
- bool relayState = false;
- //----------------------------------------------------------------------------------------------//
- volatile boolean fanON = 0;
- bool machineState = false;
- char Serialdata;
- void setup() {
- // Serial.begin(9600);
- pinMode(2, INPUT); // ZCD
- pinMode(digit1, OUTPUT);
- pinMode(digit2, OUTPUT);
- pinMode(relay, OUTPUT);
- pinMode(buzzer, OUTPUT);
- pinMode(clock, OUTPUT);
- pinMode(data, OUTPUT);
- pinMode(MotorPin, OUTPUT);
- fan = false;
- attachInterrupt(0, dimmer, FALLING);
- tens = 13; // ALL on
- unit = 13;
- delay(500);
- tens = 14; // All off
- unit = 14;
- // EEPROM.write(10, 2); // Enable for force close swing
- }
- //----------------------------------------------------------------------------------------------//
- void beep() {
- digitalWrite(buzzer, HIGH); // turn the LED on (HIGH is the voltage level)
- delay(100); // wait for a second
- digitalWrite(buzzer, LOW); // turn the LED off by making the voltage LOW
- delay(100);
- }
- //----------------------------------------------------------------------------------------------//
- // Stepper
- void swingOut() { // Open
- EEPROM.write(10, 1);
- StepsRequired = - STEPS_PER_OUT_REV / 2;
- steppermotor.setSpeed(700);
- steppermotor.step(StepsRequired);
- }
- void swingIn() { // Close
- EEPROM.write(10, 0xFF);
- StepsRequired = STEPS_PER_OUT_REV / 2;
- steppermotor.setSpeed(700);
- steppermotor.step(StepsRequired);
- }
- //----------------------------------------------------------------------------------------------//
- void dimmer()
- {
- long int dimval;
- dimval = timeval * 75;
- if (fanON) startTimer1(dimval);
- switch (digNum)
- {
- case 1:
- digitalWrite(digit2, LOW);
- shiftOut(data, clock, LSBFIRST, Digit[tens]);
- digitalWrite(digit1, HIGH);
- break;
- case 2:
- digitalWrite(digit1, LOW);
- shiftOut(data, clock, LSBFIRST, Digit[unit]);
- digitalWrite(digit2, HIGH);
- break;
- }
- digNum = digNum + 1;
- if (digNum > 2)
- digNum = 1;
- }
- //----------------------------------------------------------------------------------------------//
- ISR(timer1Event)
- {
- pauseTimer1();
- digitalWrite(MotorPin, HIGH);
- delayMicroseconds(100);
- digitalWrite(MotorPin, LOW);
- }
- void loop() {
- if (Serial.available() > 0) {
- Serialdata = Serial.read();
- //////////////////////////////////////////////////////////////////// ON ////////////////////////////////////////////////////////////////////
- if (Serialdata == '1')
- {
- if (machineState == false) { // Turn ON only if it's OFF
- // Serial.println("ON");
- tens = 0; // "0n" Segment display
- unit = 15;
- beep();
- digitalWrite(relay, HIGH); // relay ON
- relayState = true;
- PreviousSwingState = EEPROM.read(10); // Add variable for address
- if (swingState == LOW && PreviousSwingState == 0xFF) { // Open Swing if closed
- swingOut();
- swingState = HIGH;
- }
- speedCounter = 3;
- timeval = fanval[2]; // Motor Top Speed
- if (interruptState == false) {
- fanON = 1;
- interruptState = true;
- }
- tens = 11; // "L1" Segment display
- unit = 1;
- machineState = true;
- }
- }
- //////////////////////////////////////////////////////////////////// OFF ////////////////////////////////////////////////////////////////////
- if (Serialdata == '2')
- {
- if (machineState == true) { // Turn OFF only if it's ON
- // Serial.println("OFF");
- beep();
- tens = 0; // Display Nothing
- unit = 10;
- //detachInterrupt(0); // Motor OFF
- fanON = 0;
- interruptState = false;
- digitalWrite(relay , LOW);
- PreviousSwingState = EEPROM.read(10);
- if (PreviousSwingState == 1) {
- swingIn();
- swingState = LOW;
- }
- tens = 14; // Display Nothing
- unit = 14;
- machineState = false;
- }
- }
- //////////////////////////////////////////////////////////////////// relay ON/OFF ALONE [PLASMA] ////////////////////////////////////////////////////////////////////
- if (Serialdata == '3')
- {
- // Serial.println("PLASMA");
- beep();
- relayState = !relayState;
- digitalWrite(relay, relayState);
- if (relayState) {
- tens = 11; // Display "L1"
- unit = 1;
- timeval = fanval[2]; // Turn ON motor if OFF
- if (interruptState == false) {
- fanON = 1;
- interruptState = true;
- }
- PreviousSwingState = EEPROM.read(10);
- if (swingState == LOW && PreviousSwingState == 0xFF) { // Open Swing if closed
- swingOut();
- swingState = HIGH;
- }
- }
- if (!relayState) {
- tens = 11; // Display "L0"
- unit = 0;
- }
- }
- //////////////////////////////////////////////////////////////////// FAN SPEED ROTATE [JET COOL] ////////////////////////////////////////////////////////////////////
- if (Serialdata == '4')
- {
- // Serial.println("JET COOL");
- beep();
- speedCounter++;
- if (speedCounter >= 3)
- speedCounter = 0;
- timeval = fanval[speedCounter]; // Motor Top Speed
- if (interruptState == false) {
- //attachInterrupt(0,dimmer,FALLING);
- fanON = 1;
- interruptState = true;
- }
- PreviousSwingState = EEPROM.read(10);
- if (swingState == LOW && PreviousSwingState == 0xFF) {
- swingOut();
- swingState = HIGH;
- }
- tens = 5; // Display "S1"
- unit = speedCounter;
- }
- //////////////////////////////////////////////////////////////////// [FAN SPEED] ////////////////////////////////////////////////////////////////////
- if (Serialdata == '5')
- {
- // Serial.println("FAN SPEED"); // Fan Speed
- // beep();
- // TODO ?
- }
- //////////////////////////////////////////////////////////////////// [TEMP + -] ////////////////////////////////////////////////////////////////////
- if (Serialdata == '6')
- {
- // Serial.println("TEMP 1");
- // beep();
- // TODO ?
- }
- if (Serialdata == '7')
- {
- // Serial.println("TEMP 2");
- // beep();
- // TODO ?
- }
- if (Serialdata == '8')
- {
- // Serial.println("TEMP 3");
- // beep();
- // TODO ?
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement