Jun 292014
 

This is the LSD03 sketch

 

 

/*  
 LSD 03 : using 6 in / out velleman card
          and arduino
          to control LSD 02 analogue circuits
          with serial console opt
          
          2014-06-29 Alex The Engineer
          rtn telecommunicatie
          VFRmedia Ipswich, UK
*/ 


const int sigPins[6] = {2,3,4,5,6,7};
const int relayPins[6] = {8,9,10,11,12,13};

// this ia LDR brightness if telephone strobe is on 

const int strobeVal = 700 ; 
const int sigDelay = 20 ; // button deley


// waakkat

#define FATCAT        1500
#define SIAMEES       375
#define KITTEN        50

// max pins (1 more than array bounds)
#define TOTALPINS       6

// beware this lib name is changed

#include <Bounce2.h>
#include <Ansiterm.h>

// global declarations & vars

Bounce sig[TOTALPINS] = {Bounce(),Bounce(),Bounce(),Bounce(),Bounce(),Bounce()} ; 
// array of bounce objects
Ansiterm ansi; // console

int incomingbyte = 0;
int relayState[6] = {  LOW, LOW, LOW, LOW, LOW, LOW};
int sigChange[6] = {   LOW, LOW, LOW, LOW, LOW, LOW };
int sigState[6] = {  HIGH, HIGH, HIGH, HIGH , HIGH, HIGH }; // as these are pulled up (active low)
int ttyUpdate = HIGH ;
int iptChange = HIGH ; // so the display always updates even if some faders already are up

unsigned long previousMillis = 0;
unsigned long lastTime = 0 ;

// waakkat

int miauw= LOW ; 
long miauwInterval =  FATCAT ; // meower 


// redlight pfl telephone

int redlight = LOW;
int pfl = LOW; // green ()
int currpfl = LOW; // for auto force green when red
int telephone = LOW ;
int amber = LOW ;

char *sigNames[]={ "RED", "PFL", "PC2", "JNG", "GRM", "TEL" } ;
int sigbColours[6] = { RED, BLUE, YELLOW, YELLOW, GREEN, WHITE };
int sigfColours[6] = { YELLOW, YELLOW, RED, RED, RED, BLUE };
char *rlyNames[]={ "MIC", "PFL", "AM1", "AM2", "SR1", "RNG" };
int rlybColours[6] = { RED, BLUE, YELLOW, YELLOW, GREEN, BLUE};
int rlyfColours[6] = { YELLOW, WHITE, BLUE, BLUE, BLUE, WHITE};

void setup()
{

  Serial.begin(38400);
  
  for (int i=0 ; i < TOTALPINS ; i++) 
  {  
      
      pinMode(sigPins[i], INPUT);
      sig[i]=Bounce();
      sig[i].attach(sigPins[i]);
      sig[i].interval(sigDelay);   
      pinMode(relayPins[i], OUTPUT);
  }

  setConsole("*rtn telecommunicatie:LSD03 1.00:ready ");
  getSignals(TOTALPINS);
  
  // but force iptchange so console updates
  // and catch any active signals on first run
  
  for ( int i=0 ; i < TOTALPINS ; i++)
  {
    relayState[i]=sigState[i];
  }
    
  iptChange=HIGH;
  
  relayEmit(TOTALPINS);
  updateConsole(TOTALPINS);
  
}


void loop() 
{

      ttyUpdate = LOW ;
      getSignals(TOTALPINS);
   
      redlight = sigState[0];
      
      if (sigState[1] && sigChange[1]) {
        pfl = !pfl ;
        currpfl = pfl;
     
         }
      
      if (redlight) {
         pfl = LOW;
         miauwInterval = SIAMEES ;
         
        }
      else {
         pfl = currpfl;
         miauwInterval = FATCAT ;

        }
      
      relayState[0] = redlight ;
      relayState[1] = pfl ;
      relayState[2] = sigState[2];
      relayState[3] = sigState[3];
      relayState[4] = sigState[4];
      relayState[5] = sigState[5];
      
      
      ttyUpdate = iptChange ;
  
// guard cat will meow to the console (tty)
  
  unsigned long currentMillis = millis();

  if (currentMillis - previousMillis > miauwInterval) {
    previousMillis = currentMillis;  
    miauw = !miauw ;      
  //  relayState[5] = miauw ;
    
    ttyUpdate = HIGH ;
    }

  relayEmit(TOTALPINS);
  
// update if there is a button change or a miauw

//  ttyUpdate = ttyUpdate | iptChange ; 
  
  while (ttyUpdate) 
    {
      updateConsole(TOTALPINS);
      ttyUpdate = LOW;
    }
   
}

// Functions now go here at end of sketch

void setConsole(char* message) 
{
  ansi.setBackgroundColor(BLACK);
  ansi.eraseScreen();//works
  ansi.home();
  ansi.setBackgroundColor(GREEN);
  ansi.setForegroundColor(BLACK);
  Serial.print(message);
}

void relayEmit(int maxPins)
{
  for (int i = 0 ; i < (maxPins) ; i++)
  {
    digitalWrite(relayPins[i],relayState[i]);

  }
}

void getSignals(int maxPins)
{
   iptChange = LOW ;
   
    for (int i = 0 ; i < (maxPins) ; i++)
  {
   sigChange[i] = sig[i].update();
   iptChange = iptChange | sigChange[i]; // have any changed?
   sigState[i] = !(sig[i].read());
  }
 }
 
 void updateConsole(int maxPins)
  { 
    if (iptChange) {
      
      for (int i=0 ; i < (maxPins) ; i++)
          {
            ansi.xy(6+(i*5),3);
            if (sigState[i])  {
                  ansi.setForegroundColor(sigfColours[i]);
                  ansi.setBackgroundColor(sigbColours[i]);
                  Serial.print(sigNames[i]);
               }
              else {
                  ansi.setForegroundColor(WHITE);
                  ansi.setBackgroundColor(BLACK);
                  Serial.print("   ");        
              }
            ansi.xy(6+(i*5),5);
              if (relayState[i])  {
                  ansi.setForegroundColor(rlyfColours[i]);
                  ansi.setBackgroundColor(rlybColours[i]);
                  Serial.print(rlyNames[i]);
                 }
            else {
                  ansi.setForegroundColor(WHITE);
                  ansi.setBackgroundColor(BLACK);
                  Serial.print("   ");        
              }
          }
         
    }              
         
          ansi.xy(34,1);
          if (miauw) {
 
              ansi.setForegroundColor(BLUE);
              ansi.setBackgroundColor(YELLOW);
              Serial.write("miauw");
             }
             else {

               ansi.setForegroundColor(WHITE);
              ansi.setBackgroundColor(GREEN);
              Serial.write("     ");        
             }
     
     }