I've been trying to get my IO expansion loaded up with both inputs and outputs, but I am struggling trying to modify the flow meter code originally posted. I'd like to add a couple ATOs as level warning sensors (one in the overflow and one in the water reservoir). These would be additional INPUTs from the IO (along with the 3 flow sensors). I also would like to move the buzzer alarm as an OUTPUT to the IO. I am trying to just send a number to trigger the alarm or ato state. Maybe there is a better way to do this, but I'm still pretty green at this coding and I really don't understand the interrupts, so I'm kind of tripping over myself.
Here is the test master code I am trying to test the IO...
Code: Select all
#include <ReefAngel_Features.h>
#include <RA_Colors.h>
#include <RA_CustomColors.h>
#include <Globals.h>
#include <RA_Wifi.h>
#include <Wire.h>
#include <OneWire.h>
#include <Time.h>
#include <DS1307RTC.h>
#include <InternalEEPROM.h>
#include <RA_NokiaLCD.h>
#include <RA_ATO.h>
#include <RA_Joystick.h>
#include <LED.h>
#include <RA_TempSensor.h>
#include <Relay.h>
#include <RA_PWM.h>
#include <Timer.h>
#include <Memory.h>
#include <ReefAngel.h>
byte IOIn[10];
int Flow0;
int Flow1;
int Flow2;
byte buzz = 0;
byte Overflow;
byte Reservoir;
void setup()
{
ReefAngel.Init();
}
void loop()
{
ReefAngel.Refresh();
GetFlow();
ReefAngel.LCD.DrawSingleMonitor(Flow0, COLOR_BLACK, 5, 10, 1);
ReefAngel.LCD.DrawSingleMonitor(Flow1, COLOR_BLACK, 5, 20, 1);
ReefAngel.LCD.DrawSingleMonitor(Flow2, COLOR_BLACK, 5, 30, 1);
ReefAngel.LCD.DrawSingleMonitor(Overflow, COLOR_BLACK, 5, 40, 1);
ReefAngel.LCD.DrawSingleMonitor(Reservoir, COLOR_BLACK, 5, 50, 1);
ReefAngel.LCD.DrawSingleMonitor(buzz, COLOR_BLACK, 5, 60, 1);
if (Overflow == 1)
{
buzz == 1;
}
else
{
buzz == 0;
}
//send data parameter package to the auxillary monitor
Wire.beginTransmission(9); // transmit to device #9
Wire.write('$'); // send the $$$
Wire.write('$');
Wire.write('$');
Wire.write(buzz);
Wire.endTransmission(); // stop transmitting
}
void GetFlow()
{
Wire.requestFrom(9,9);
if(Wire.available())
{
for (int a=0;a<10;a++)
IOIn[a]=Wire.read();
}
Flow0=(IOIn[1]<<8)+IOIn[2];
Flow1=(IOIn[3]<<8)+IOIn[4];
Flow2=(IOIn[5]<<8)+IOIn[6];
Overflow=IOIn[7];
Reservoir=IOIn[8];
}
and the test IO slave code...
Code: Select all
#include <Wire.h>
#include <avr/wdt.h>
//wire.write variable number
byte param[2];
byte IOports3;
byte IOports5;
byte IOports6;
byte IOports9;
byte IOports10;
byte IOports11;
byte Overflow=0;
byte Reservoir=0;
byte Buzzer=0;
byte IOOut[7];
volatile int Flow0 = 0;
volatile int Flow1 = 0;
volatile int Flow2 = 0;
unsigned long lastmillis=millis();
void FlowMeter1()
{
Flow1++;
}
SIGNAL(PCINT0_vect) {
Flow0++;
}
SIGNAL(PCINT2_vect) {
Flow2++;
}
void paramsend(int a)
{
param[0]=(byte)(a>>8);
param[1]=(byte)(a);
Wire.write(param,2);
}
void setup()
{
// Serial.begin(57600);
Wire.begin(9);
Wire.onRequest(requestEvent);
pinMode(IOports3,INPUT);
digitalWrite(IOports3,HIGH); //pull up resistor
pinMode(IOports5,INPUT);
digitalWrite(IOports5,HIGH); //pull up resistor
pinMode(IOports6,INPUT);
digitalWrite(IOports6,LOW); //pull up resistor
pinMode(IOports9,INPUT);
digitalWrite(IOports9,LOW); //pull up resistor
pinMode(IOports10,OUTPUT);
digitalWrite(IOports10,LOW); //pull up resistor
pinMode(IOports11,INPUT);
digitalWrite(IOports11,HIGH); //pull up resistor
PCMSK0 |= (1<<PCINT3); // Pin 11
PCICR |= (1<<PCIE0); // Interrupt 0
PCMSK2 |= (1<<PCINT21); // Pin 5
PCICR |= (1<<PCIE2); // Interrupt 2
MCUCR = (1<<ISC01) | (1<<ISC01); //Rising edge
attachInterrupt(1, FlowMeter1, RISING); // Interrupt 1
wdt_enable(WDTO_1S);
}
void loop()
{
wdt_reset();
if (millis()-lastmillis>1000)
{
lastmillis=millis();
IOOut[1]=Flow0>>8;
IOOut[2]=Flow0;
IOOut[3]=Flow1>>8;
IOOut[4]=Flow1;
IOOut[5]=Flow2>>8;
IOOut[6]=Flow2;
Flow0=0;
Flow1=0;
Flow2=0;
Overflow=0;
Reservoir=0;
}
//overflow sensor
if (digitalRead(IOports6) == HIGH)
{
Overflow == 1;
}
else
{
Overflow == 0;
}
//reservoir sensor
if (digitalRead(IOports9) == HIGH)
{
Reservoir == 1;
}
else
{
Reservoir == 0;
}
//overtemp alarm
if (Buzzer = 1)
{
digitalWrite(IOports10,HIGH);
}
else
{
digitalWrite(IOports10,LOW);
}
}
void requestEvent() {
paramsend(Flow0);
paramsend(Flow1);
paramsend(Flow2);
Wire.write(Overflow);
Wire.write(Reservoir);
}
void receiveEvent(int howMany)
{
if (howMany==4) // Our custom protocol is 4 bytes
{
byte cmd1,cmd2,cmd3,cmd4;
cmd1=Wire.read();
cmd2=Wire.read();
cmd3=Wire.read();
cmd4=Wire.read();
if (cmd1=='$' && cmd2=='$' && cmd3=='$') // the first 3 bytes of the custom protocol are $$$ to ensure it's coming from RA
{
Buzzer=cmd4;
}
}
else
for (int a=0;a<howMany;a++)
Wire.read(); // if the number of bytes is not 4, discard everything
}
right now I get a screen like this:
0
0
0
255
255
0
and no response. Since it isn't hooked up to any sensors the buzzer is just triggered by a the ato singal in the master (kind of a circular aresponse, but it's jsut a test) I've had success sending info before, but I'm getting confused with this one. Anyhelp would be greatly appreciated.
-Jon