MHV sign code


(ian) #1

Putting this here for safe keeping

const int DoorInput = 2;
const int PowerOutput = 3;
const int Relay0Output = 4;
const int Relay1Output = 5;
const int Relay2Output = 6;
const int Relay3Output = 7;
const int PowerOff = HIGH;
const int PowerOn = LOW;
const int RelayOff = HIGH;
const int RelayOn = LOW;
const int PowerOnDelay = 1000;
const int RelayOnDelay = 1000;
const int PowerOffDelay = 1000;
const int RelayOffDelay = 1000;
const int SignOnPeriod = 3000;
const int SignOffPeriod = 2000;
const int SignalDelay = 333;
const int SignalEndDelay = 1000;
const int TriggerDelay = 7000;

unsigned long TimeRemaining = 0;
unsigned long LastTime = 0;
bool DoorOpened = false;
unsigned long SignStateTime = 0;
bool SignOn = false;

const int ClosedSignal = 4;
const int OpenSignal = 3;
const int DoorClosedSignal = 2;
const int DoorOpenSignal = 1;

void setup() 
{
  pinMode(PowerOutput, OUTPUT);
  digitalWrite(PowerOutput, PowerOff);
  
  pinMode(Relay0Output, OUTPUT);
  digitalWrite(Relay0Output, RelayOff);
  
  pinMode(Relay1Output, OUTPUT);
  digitalWrite(Relay1Output, RelayOff);
  
  pinMode(Relay2Output, OUTPUT);
  digitalWrite(Relay2Output, RelayOff);
  
  pinMode(Relay3Output, OUTPUT);
  digitalWrite(Relay3Output, RelayOff);
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  
  pinMode(DoorInput, INPUT);
  digitalWrite(DoorInput, HIGH);

  WaitTriggerDelay();
}

void loop() 
{
  unsigned long currentTime = millis();
  unsigned long timeDifference = currentTime - LastTime;
  if(timeDifference > 200)
  {
    timeDifference = 0;
  }else
  {
    if(TimeRemaining > 0)
    {
      if(SignStateTime > timeDifference)
      {
        SignStateTime = SignStateTime - timeDifference;
      }else if(SignStateTime > 0)
      {
        if(SignOn)
        {
          digitalWrite(Relay0Output, RelayOff);
          SignOn = false;
          SignStateTime = SignOffPeriod;
        }else 
        {
          digitalWrite(Relay0Output, RelayOn);
          SignOn = true;
          SignStateTime = SignOnPeriod;
        }
      }
    }
    
    if(TimeRemaining > timeDifference)
    {
      TimeRemaining = TimeRemaining - timeDifference;
    }else if(TimeRemaining > 0)
    {
        digitalWrite(LED_BUILTIN, LOW);
        SendSignal(ClosedSignal);
        
        digitalWrite(Relay0Output, RelayOff);
        SignOn = false;
        SignStateTime = 0;

        digitalWrite(Relay1Output, RelayOff);
        delay(RelayOffDelay);

        digitalWrite(Relay2Output, RelayOff);
        delay(RelayOffDelay);

        digitalWrite(PowerOutput, PowerOff);
        delay(PowerOffDelay);

        TimeRemaining = 0;
    }
  }
  LastTime = currentTime;

  int input = ReadInput(DoorOpened);
  if(input)
  {
    if(TimeRemaining == 0)
    {
      WaitTriggerDelay();

      digitalWrite(LED_BUILTIN, HIGH);

      digitalWrite(PowerOutput, PowerOn);
      delay(PowerOnDelay);

      digitalWrite(Relay0Output, RelayOn);
      SignOn = true;
      SignStateTime = SignOnPeriod;

      SendSignal(OpenSignal);

      digitalWrite(Relay0Output, RelayOn);
      delay(RelayOnDelay);

      digitalWrite(Relay1Output, RelayOn);
      delay(RelayOnDelay);

      digitalWrite(Relay2Output, RelayOn);
      delay(RelayOnDelay);
    }else
    {
      if (!DoorOpened)
      {
          SendSignal(DoorOpenSignal);
      }
    }

    TimeRemaining = 1l * 60l * 60l * 1000l;
    //TimeRemaining = 5 * 1000;
    DoorOpened = true;
  }else
  {
      if (DoorOpened)
      {
          SendSignal(DoorClosedSignal);
      }
      DoorOpened = false;
  }
}

int ReadInput(int currentInput)
{
  int firstState = digitalRead(DoorInput);
  delay(100);

  int secondState = digitalRead(DoorInput);
  if(secondState != firstState)
  {
    return currentInput;
  }

  return firstState;
}


void WaitTriggerDelay()
{
  unsigned long waitTime = TriggerDelay;
  while(waitTime > 0)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    
    if(waitTime > 1000)
    {
      waitTime = waitTime - 1000;
    }else
    {
      waitTime = 0;
    }
  }
}

void SendSignal(int count)
{
  while(count > 0)
  {
    digitalWrite(Relay3Output, RelayOn);
    delay(SignalDelay);

    digitalWrite(Relay3Output, RelayOff);
    if(count > 1)
    {
      delay(SignalDelay);
    }else
    {
      delay(SignalEndDelay);
    }
    count = count - 1;
  }
}

(ian) #2

Changes for controlling game machine.

Drivers can be found at
http://www.wch.cn/download/CH341SER_EXE.html
also see
https://forum.arduino.cc/index.php?topic=397368.0

const int DoorInput = 2;
const int PCInput = 10;

const int PowerOutput = 3;

const int SignRelayOutput = 4;
const int AccessoriesARelayOutput = 5;
const int PCRelayOutput = 6;
const int SignalRelayOutput = 7;

const int PowerOff = HIGH;
const int PowerOn = LOW;
const int RelayOff = HIGH;
const int RelayOn = LOW;

const unsigned long PowerOnDelay = 1000l;
const unsigned long PowerOffDelay = 1000l;

const unsigned long RelayOnDelay = 1000l;
const unsigned long RelayOffDelay = 1000l;
const unsigned long SignalDelay = 333l;
const unsigned long SignalEndDelay = 1000l;
const unsigned long TriggerDelay = 10 * 1000l;
const unsigned long PCControlRelayDelay = 1 * 1000l;


const unsigned long ActivePeriod = 1l * 60l * 60l * 1000l;
const unsigned long SignOnPeriod = 3 * 1000l;
const unsigned long SignOffPeriod = 2 * 1000l;

const unsigned long AccessoriesAOnPeriod = 5 * 60l * 60l * 1000l;
const unsigned long AccessoriesAOffPeriod = 3 * 60l * 1000l;

const int ClosedSignal = 4;
const int OpenSignal = 3;
const int DoorClosedSignal = 2;
const int DoorOpenSignal = 1;

unsigned long TimeRemaining = 0;
unsigned long LastTime = 0;
bool DoorOpened = false;

bool SignOn = false;
unsigned long SignStateTime = 0;

bool AccessoriesAOn = false;
unsigned long AccessoriesATime = 0;

void setup() 
{
  pinMode(PowerOutput, OUTPUT);
  digitalWrite(PowerOutput, PowerOff);
  
  pinMode(SignRelayOutput, OUTPUT);
  digitalWrite(SignRelayOutput, RelayOff);
  
  pinMode(AccessoriesARelayOutput, OUTPUT);
  digitalWrite(AccessoriesARelayOutput, RelayOff);
  
  pinMode(PCRelayOutput, OUTPUT);
  digitalWrite(PCRelayOutput, RelayOff);
  
  pinMode(SignalRelayOutput, OUTPUT);
  digitalWrite(SignalRelayOutput, RelayOff);
  
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, LOW);
  
  pinMode(DoorInput, INPUT);
  digitalWrite(DoorInput, HIGH);

  pinMode(PCInput, INPUT);
  digitalWrite(PCInput, LOW);

  WaitTriggerDelay();

  // wait until door closed
  while(true)
  {
      int input = ReadInput(DoorOpened);
      if(!input)
      {
        break;
      }
  }
}


void loop() 
{
  unsigned long currentTime = millis();
  unsigned long timeDifference = currentTime - LastTime;

  if(timeDifference > 200)
  {
    timeDifference = 0;
  }else
  {
    if(TimeRemaining > 0)
    {
      if(SignStateTime > timeDifference)
      {
        SignStateTime = SignStateTime - timeDifference;
      }else
      {
        if(SignOn)
        {
          digitalWrite(SignRelayOutput, RelayOff);
          SignOn = false;
          SignStateTime = SignOffPeriod;
        }else 
        {
          digitalWrite(SignRelayOutput, RelayOn);
          SignOn = true;
          SignStateTime = SignOnPeriod;
        }
      }

      if(AccessoriesATime > timeDifference)
      {
        AccessoriesATime = AccessoriesATime - timeDifference;
      }else
      {
        if(AccessoriesAOn)
        {
          //digitalWrite(AccessoriesARelayOutput, RelayOff);
          AccessoriesAOn = false;
          AccessoriesATime = AccessoriesAOffPeriod;
        }else 
        {
          //digitalWrite(AccessoriesARelayOutput, RelayOn);
          AccessoriesAOn = true;
          AccessoriesATime = AccessoriesAOnPeriod;
        }
      }
    }else
    {
      // off
    }
    
    if(TimeRemaining > timeDifference)
    {
      TimeRemaining = TimeRemaining - timeDifference;
    }else if(TimeRemaining > 0)
    {
        // turn off
        
        digitalWrite(LED_BUILTIN, LOW);
        SendSignal(ClosedSignal);
        
        digitalWrite(SignRelayOutput, RelayOff);
        SignOn = false;
        SignStateTime = 0;
        AccessoriesAOn = false;
        AccessoriesATime = 0;
        delay(RelayOffDelay);

        digitalWrite(AccessoriesARelayOutput, RelayOff);
        delay(RelayOffDelay);

        digitalWrite(PowerOutput, PowerOff);
        delay(PowerOffDelay);

        if (ReadPC())
        {
          digitalWrite(PCRelayOutput, RelayOn);
          delay(PCControlRelayDelay);
          digitalWrite(PCRelayOutput, RelayOff);
          delay(RelayOffDelay);
        }

        TimeRemaining = 0;
    }else
    {
      // idle - off
    }
  }

  LastTime = currentTime;

  int input = ReadInput(DoorOpened);
  if(input)
  {
    if(TimeRemaining == 0)
    {
      // turn on
      WaitTriggerDelay();

      digitalWrite(LED_BUILTIN, HIGH);

      digitalWrite(PowerOutput, PowerOn);
      delay(PowerOnDelay);

      digitalWrite(SignRelayOutput, RelayOn);
      SignOn = true;
      SignStateTime = SignOnPeriod;
      delay(RelayOnDelay); 

      AccessoriesAOn = false;
      AccessoriesATime = AccessoriesAOffPeriod;

      digitalWrite(AccessoriesARelayOutput, RelayOn);
      delay(RelayOnDelay);

        if (!ReadPC())
        {
          digitalWrite(PCRelayOutput, RelayOn);
          delay(PCControlRelayDelay);
          digitalWrite(PCRelayOutput, RelayOff);
          delay(RelayOnDelay);
        }

      SendSignal(OpenSignal);
    }else
    {
      if (!DoorOpened)
      {
          SendSignal(DoorOpenSignal);
      }
    }

    TimeRemaining = ActivePeriod;
    DoorOpened = true;
  }else
  {
      if (DoorOpened)
      {
          SendSignal(DoorClosedSignal);
      }
      DoorOpened = false;
  }
}


int ReadInput(int currentInput)
{
  int firstState = digitalRead(DoorInput);
  delay(100);

  int secondState = digitalRead(DoorInput);
  if(secondState != firstState)
  {
    return currentInput;
  }

  return firstState;
}


int ReadPC()
{
  return digitalRead(PCInput);
}


void WaitTriggerDelay()
{
  unsigned long waitTime = TriggerDelay;
  while(waitTime > 0)
  {
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    
    digitalWrite(LED_BUILTIN, LOW);
    delay(500);
    
    if(waitTime > 1000)
    {
      waitTime = waitTime - 1000;
    }else
    {
      waitTime = 0;
    }
  }
}


void SendSignal(int count)
{
  return ;
  while(count > 0)
  {
    digitalWrite(SignalRelayOutput, RelayOn);
    delay(SignalDelay);

    digitalWrite(SignalRelayOutput, RelayOff);
    if(count > 1)
    {
      delay(SignalDelay);
    }else
    {
      delay(SignalEndDelay);
    }
    count = count - 1;
  }
}