OpenAccess I/O functions

One of the first things I did was give manual control of the relays to the serial port, that way the logic could be moved completely offboard if it was desired.

There are some maps between relay and analog pins and which relay/analog they are connected to:

//pin-mappings
const int relaysToPins[8] = {
31,32,33,34,35,36,37,38};
const int analogToPins[16] = {
54,55,56,57,58,59,60,61,62,63,64,65,66,67,68,69};

These hold the variables of the different relays (so they can be checked in a non-blocking way)

//toggle stuff
//unsigned long max_toggle = 15000; //maximum amount of time a toggle can seize up the processor with a delay loop
unsigned long toggle_dur[8] = {
0,0,0,0,0,0,0,0}; //time in thousandths of a second
unsigned long toggle_start[8] = {
0,0,0,0,0,0,0,0}; //start time
boolean toggled[8] = {
0,0,0,0,0,0,0,0}; //active or not

This initializes my I/O

void IO_init()
{
for (int i=0;i<sizeof(relaysToPins)/sizeof(relaysToPins[0]);i++)
{
pinMode(relaysToPins[i], OUTPUT);
}
for (int i=0;i<sizeof(analogToPins)/sizeof(analogToPins[0]);i++)
{
pinMode(analogToPins[i], INPUT);//not needed?
}
pinMode(exit_button,INPUT_PULLUP);//pullup not needed?
}

These are the simple on and off commands:

// Turns relay on
//format: ON;<relay number(not pin number)>
void SCmd_relay_on()
{
char *arg;
arg = SCmd.next();
if (arg != NULL && atoi(arg) < 8 && ((atoi(arg) == exit_relay && exiting == 1) ? 0 : 1)) //protect exit relay
{
digitalWrite(relaysToPins[atoi(arg)],HIGH);
}
}
// Turns relay off
//format: OFF;<relay number(not pin number)>
void SCmd_relay_off()
{
char *arg;
arg = SCmd.next();
if (arg != NULL && atoi(arg) < 8 && ((atoi(arg) == exit_relay && exiting == 1) ? 0 : 1)) //protect exit relay
{
digitalWrite(relaysToPins[atoi(arg)],LOW);
}
}

Here is how I take commands to toggle relays:

// Toggles relay with duration
//format: TOGGLE;<relay number(not pin number)>;<time to be on for in a ‘delay(arg)’ format>
void SCmd_toggle()
{
int relay;
int dur;
char *arg;
//Serial.println(“toggle”);
arg = SCmd.next();
if (arg != NULL && atoi(arg) < 8 && ((atoi(arg) == exit_relay && exiting == 1) ? 0 : 1))
{
relay=atoi(arg); // Converts a char string to an integer
//Serial.print(“relay no: “);
//Serial.println(relay);
}
else
{
//Serial.println(“No arguments”);
}
arg = SCmd.next();
if (arg != NULL)
{
dur=atoi(arg);
//Serial.print(“Duration: “);
//Serial.println(dur);
digitalWrite(relaysToPins[relay], HIGH);
toggled[relay] = 1;
toggle_dur[relay] = dur;
toggle_start[relay] = millis();
}
else
{
//Serial.println(“No second argument”);
}
}

The I/O may not seem important, but it’s there so why not use it.  This is part of the larger OpenAccess project.

2 Responses to “OpenAccess I/O functions”

  1. OpenAccess Exit button/relay | Evan's Techie-Blog Says:

    […] is to the exit button and exit relay code.  It can also be called like any other relay from the I/O section, but it operates on its own unlike the others.  This is part of the OpenAccess […]

  2. OpenAccess 3.0 replacement software | Evan's Techie-Blog Says:

    […] I/O functions […]

Leave a comment