|
|
|
|
|
|
|
Arduino based anti thermal/flood system (Ship-Tect).
********** I have gone through and made all programming comments (denoted by //) bold and italicized for easy recognition
. This will be done with every posted code update from now on ![]() A while ago, I started a thread on an Arduino based ship safety system (encompassing thermal and flood control). The link to the original thread is here: http://www.rcgroups.com/forums/showt...ighlight=decki This thread will provide a continuation of that project within the Homebrew Hacks section (the most appropriate location). Below is a diagram of the original vision, and the latest version of the code, SHIP-TECT 0.3b Mega. Current development is focusing on the addition of a menu allowing change of basic settings, calbration, and the ability to enable/disable the different functions, as well as the ability to have an Arduino Uno connected via I2C for water ballast control in multiple tanks. Basic Slave sender code has been implemented and some keypad control code is implemented. As you can see, the concept is ever-evolving ![]() Links to the major components used in the development hardware: Freetronics Ethermega 2560 (100% Arduino Mega compatible Freetronics Arduino Eleven 100% Uno compatible Freetronics LCD and Keypad Shield Freetronics 8 Channel Relay Driver Shield Please find the Main Mega code below, and the Slave Arduino Uno code below that: Main Arduino Mega code: ************************************************** ****** Code:
// SHIP-TECT Emergency Management v0.3bMega. By Michael Ryan Judge, Licensed under Creative Commons, 2013 for Arduino Uno.
//Pins used by LCD & Keypad Shield:
// A0: Buttons, analog input from voltage ladder
// D4: LCD bit 4
// D5: LCD bit 5
// D6: LCD bit 6
// D7: LCD bit 7
// D8: LCD RS
// D9: LCD E
// D3: LCD Backlight (high = on, also has pullup high so default is on)
//ADC voltages for the 5 buttons on analog input pin A0:
// RIGHT: 0.00V : 0 @ 8bit ; 0 @ 10 bit
// UP: 0.71V : 36 @ 8bit ; 145 @ 10 bit
// DOWN: 1.61V : 82 @ 8bit ; 329 @ 10 bit
// LEFT: 2.47V : 126 @ 8bit ; 505 @ 10 bit
// SELECT: 3.62V : 185 @ 8bit ; 741 @ 10 bit
// Can include temperature monitoring for up to 3 main engines realistically (just cut, paste and rename values for each motor) thermal cut-out of the main engine (and any other additional main engines or motors), monitoring of the water cooling (keel cooling in this version) system efficiency. Working Relay support is for the
//Freetronics 8 channel relayshield or "Relay8", which the code has been test with, on a Freetronics Arduino Mega with Freetronics 16x2 LCD and Keypad shield, and Relay 8 shield. Will leave all systems running in the event
//of an error condition, and will sound the horn 5 times and then once every two seconds while printing sensor data, then repeat.
#include <LiquidCrystal.h> // include LCD library
#include <Wire.h>
#define I2C_ADDR 0x20 // 0x20 is the address with all jumpers removed
#define LCD_BACKLIGHT_PIN 3 // D3 controls LCD backlight
// ADC readings expected for the 5 buttons on the ADC input
#define RIGHT_10BIT_ADC 0 // right
#define UP_10BIT_ADC 145 // up
#define DOWN_10BIT_ADC 329 // down
#define LEFT_10BIT_ADC 505 // left
#define SELECT_10BIT_ADC 741 // right
#define BUTTONHYSTERESIS 10 // hysteresis for valid button sensing window
//return values for ReadButtons()
#define BUTTON_NONE 0 //
#define BUTTON_RIGHT 1 //
#define BUTTON_UP 2 //
#define BUTTON_DOWN 3 //
#define BUTTON_LEFT 4 //
#define BUTTON_SELECT 5 //
/*--------------------------------------------------------------------------------------
Variables
--------------------------------------------------------------------------------------*/
byte buttonJustPressed = false; //this will be true after a ReadButtons() call if triggered
byte buttonJustReleased = false; //this will be true after a ReadButtons() call if triggered
byte buttonWas = BUTTON_NONE; //used by ReadButtons() for detection of button events
//Set up liquid crystal display.
LiquidCrystal lcd( 8, 9, 4, 5, 6, 7 ); //Pins for the freetronics 16x2 LCD shield. LCD: ( RS, E, LCD-D4, LCD-D5, LCD-D6, LCD-D7 )
void setup(){
//start serial connection
Serial.begin(14400);
//Wake up the I2C BUS and assign Bank a as relay outputs.
Wire.begin(); // Wake up I2C bus
// Set I/O bank A to outputs
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x00); // IODIRA register
Wire.write(0x00); // Set all of bank A to outputs
Wire.endTransmission();
//configure Flood detection pins as inputs and enable the internal pull-up resistors for each
//HIGHFLOOD
pinMode(33, INPUT_PULLUP);
//MIDFLOOD
pinMode(41, INPUT_PULLUP);
//LOWFLOOD
pinMode(47, INPUT_PULLUP);
//MOTOR TEMPERATURE SENSORs INPUTs
pinMode(A12, INPUT); //INPUT FOR MAIN ENGINE
pinMode(A13, INPUT); //INPUT FOR WATER PRIOR TO ESC COOLER
pinMode(A14, INPUT); //INPUT FOR EXTERNAL WATER TEMPERATURE
pinMode(A15, INPUT);//INPUT FOR WATER AFTER ESC COOLER
//CURRENT FEED FOR WATERLEVEL SENSORS
//All output pin modes ommitted as relay board now does the work.
//Initialise and turn on the backlight.
digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //backlight control pin D3 is high (on)
pinMode( LCD_BACKLIGHT_PIN, OUTPUT );
//Initialise the LCD display and format character usage.
lcd.begin( 16, 2 );
//Boot-up. Show software name, version and copyright.
lcd.setCursor( 0, 0 );
lcd.print( "SHIP-TECT 0.3bMega" );
lcd.setCursor( 0, 1 );
lcd.print( "M.R.JUDGE 2013" );
Serial.println("SHIP-TECT v0.3bMega, Creative Commons Michael R. Judge 2013");
delay(2000);
//Initialisation screen, not really required, but gives a more professional look to the system. 6 Cycles of the backlight
lcd.setCursor( 0, 0 );
lcd.print( "INITIALISING........");
Serial.println("INITIALISING.......");
lcd.setCursor( 0, 1);
lcd.print( " ");
//Pulse the LED Backlight on and off during initialisation.
digitalWrite( LCD_BACKLIGHT_PIN, LOW );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, HIGH ); //leave the backlight on at exit
Wire.beginTransmission(I2C_ADDR); // Test the relays during initialisation
Wire.write(0x12); // Select bank A
Wire.write(15); // Send value to bank A
Wire.endTransmission();
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, LOW );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, HIGH );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, LOW );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, HIGH );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, LOW );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, HIGH );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, LOW );
delay( 500 );
digitalWrite( LCD_BACKLIGHT_PIN, HIGH );
delay( 500 );
// Power on RC gear (normal operating state).
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
}
// Main control loop.
void loop(){
// Clear the LCD screen at each run of the loop with BLANKS.
lcd.setCursor( 0, 1);
lcd.print( " ");
[/i] [/b] //Print some blank lines for some blank space on the serial monitor to denote space between each run of the loop [/i] [/b]
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
Serial.println(" ");
// Main loop. Checks the individual pins as to whether HIGH (dry) or LOW (wet) and takes appropriate action by disabling motors and/or starting pumps depending
//on the situation, such as motor overheating or flooding.
Serial.println("Analysing.....");
lcd.setCursor( 0, 0 );
lcd.print( "Analysing.............." );
//Check for 1st stage flooding
int lowfloodVal = digitalRead(47);
Serial.println(lowfloodVal); Serial.print(" lowflood");
delay(500);
//Check for 2nd stage flooding.
int midfloodVal = digitalRead(41);
Serial.println(midfloodVal); Serial.print(" midflood");
//Check for high level flooding
delay(500);
int highfloodVal = digitalRead(33);
Serial.println(highfloodVal); Serial.print(" highflood");
//read slave arduino for bow flooding status
Wire.requestFrom(2, 1); // request 6 bytes from slave device #2
int bowfloodVal = Wire.read(); // receive a byte as character
Serial.println( "bow flooding level is " ); Serial.print(bowfloodVal); // print the character
delay(100);
//Read main motortemp
int port = analogRead(A12);
float voltageport = port * 5.0;
voltageport /= 1024.0;
float motortemp = (voltageport - 0.5) * 100;
Serial.println(" Main Engine temperature is (in Celcius) "); Serial.print(motortemp);
Serial.println(" ");
//Read intake water temp
int intake = analogRead(A13);
float voltageintake = intake * 5.0;
voltageintake /= 1024.0;
float intaketemp = (voltageintake - 0.5) * 100;
Serial.println(" Intake water temperature is in (Celcius) "); Serial.print(intaketemp);
Serial.println(" ");
//Read external water temp
int external = analogRead(A14);
float voltageexternal = external * 5.0;
voltageexternal /= 1024.0;
float externaltemp = (voltageexternal - 0.5) * 100;
Serial.println(" External water temperature is (Celcius) "); Serial.print(externaltemp);
Serial.println(" ");
//Read ESC after-cooler water temp
int esc = analogRead(A15);
float voltageexhaust = esc * 5.0;
voltageexhaust /= 1024.0;
float exhausttemp = (voltageexhaust - 0.5) * 100;
Serial.println(" Exhaust water temperature is (Celcius) "); Serial.print(externaltemp);
Serial.println(" ");
//Indicates all sensors successfully read on the LCD and Serial monitor.
lcd.setCursor( 0, 0);
lcd.print( "......DONE......" );
Serial.println("......DONE......");
delay(500);
//Calculates and Indicates ESC cooler efficiency
int escefficiency = (intaketemp / exhausttemp) * 100;
lcd.setCursor( 0, 1);
lcd.print( "ESC Eff: " ); lcd.print(escefficiency);
Serial.println("Efficiency of the ESC Cooler is "); Serial.print(escefficiency);
Serial.println(" ");
delay(500);
lcd.setCursor( 0, 1);
lcd.print( " ");
//Calculates and Indicates keel cooler efficiency
int keelefficiency = (externaltemp / intaketemp) * 100;
lcd.setCursor( 0, 1);
lcd.print( "Cooler Eff: " ); lcd.print(keelefficiency);
Serial.println("Efficiency of the Keel Cooler is "); Serial.print(keelefficiency);
Serial.println(" ");
delay(500);
lcd.setCursor( 0, 1);
lcd.print( " ");
//Calculates and Indicates overall water cooling system efficiency
int totalefficiency = (escefficiency / keelefficiency) * 100;
lcd.setCursor( 0, 1);
lcd.print( "Total Eff: " ); lcd.print(totalefficiency);
Serial.println("Total Efficiency of the Cooler is "); Serial.print(totalefficiency);
Serial.println(" ");
delay(500);
lcd.setCursor( 0, 1);
lcd.print( " ");
//If high level flooding is detected, will enable pumping, power isolation to motor and all RC gear, and ballast pump-out. From this point on it is a case of evaluating each if section of code, till we find one where all sensor values match the requirements, executing that code, then starting the main loop over again
if (lowfloodVal == LOW && midfloodVal == LOW && highfloodVal == LOW && bowfloodVal == 3) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(7); // Send value to bank A
Wire.endTransmission();
Serial.println("Stage 3 flood");
lcd.setCursor( 0, 1);
lcd.print( "Stage 3 flood");
delay(750);
lcd.setCursor( 0, 1);
lcd.print( "All pumps on........");
Serial.println("All pumps ON........");
delay(750);
lcd.setCursor( 0, 1);
lcd.print( "ALL DISABLED.....");
Serial.println("ALL GEAR DISABLED.....");
delay(750);
}
else {
// If 2nd stage flooding detected, will enable pumping and power isolation.
if (lowfloodVal == LOW && midfloodVal == LOW && highfloodVal == HIGH && bowfloodVal == 2) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(11); // Send value to bank A
Wire.endTransmission();
Serial.println("Stage 2 flood");
lcd.setCursor( 0, 1);
lcd.print( "Stage 2 flood");
delay(750);
lcd.setCursor( 0, 1);
lcd.print( "E. Pump ON.....");
Serial.println("E. Pump ON......");
delay(750);
lcd.setCursor( 0, 1);
lcd.print( "MOTORS DISABLED.....");
Serial.println("MOTORS DISABLED.....");
delay(750);
digitalWrite(31, LOW);
digitalWrite(41, LOW);
digitalWrite(47, LOW);
}
else {
// If 1st stage flooding detected, will enable pumping.
if (lowfloodVal == LOW && midfloodVal == HIGH && highfloodVal == HIGH && bowfloodVal == 1) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(10); // Send value to bank A
Wire.endTransmission();
Serial.println("Stage 1 flood");
lcd.setCursor( 0, 1);
lcd.print( "Stage 1 flood");
delay(750);
lcd.setCursor( 0, 1);
lcd.print( "E. Pump is ON.....");
Serial.println("E. Pump is ON......");
delay(750);
lcd.setCursor( 0, 1);
lcd.print( "MOTORS ENABLED.....");
Serial.println("MOTORS ENABLED.....");
delay(750);
}
else {
//If no flooding detected, but main motor overheats, ESC's OFF, all pumps off, valves closed, and all major electronics isolated.)
if (lowfloodVal == HIGH && midfloodVal == HIGH && highfloodVal == HIGH && bowfloodVal == 0 && motortemp > 0 && motortemp >= 60 && motortemp < 130 && totalefficiency > 0 && totalefficiency >= 60 && totalefficiency < 101 ) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(9); // Send value to bank A
Wire.endTransmission();
Serial.println("Nil flooding...");
lcd.setCursor( 0, 1);
lcd.print( "Nil flooding...");
delay(750);
Serial.println("E. Pump is OFF......");
lcd.setCursor( 0, 1);
lcd.print( "E. Pump is OFF......");
delay(750);
Serial.println("MOTOR OVERHEATED....."); Serial.print(motortemp);
lcd.setCursor( 0, 1);
lcd.print( "MOTOR TEMP "); lcd.print(motortemp);
delay(10000);
}
else {
//If no flooding detected, but ESC overheats, ESC's OFF, all pumps off, valves closed, and all major electronics isolated.)
if (lowfloodVal ==HIGH && midfloodVal == HIGH && highfloodVal == HIGH && bowfloodVal == 0 && motortemp > 0 && motortemp <= 59 && motortemp < 130 && totalefficiency > 0 && totalefficiency <= 59 && totalefficiency < 101 ) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(9); // Send value to bank A
Wire.endTransmission();
Serial.println("Nil flooding...");
lcd.setCursor( 0, 1);
lcd.print( "Nil flooding...");
delay(750);
Serial.println("E. Pump is OFF......");
lcd.setCursor( 0, 1);
lcd.print( "E. Pump is OFF......");
delay(750);
Serial.println("ESC OVERHEATED....."); Serial.print(totalefficiency);
lcd.setCursor( 0, 1);
lcd.print( "H/E EFF % "); lcd.print(totalefficiency);
delay(10000);
}
else {
//If no flooding detected, leave motors running and all pumps off, valves closed.)
if (lowfloodVal == HIGH && midfloodVal == HIGH && highfloodVal == HIGH && bowfloodVal == 0 && motortemp >= 1 && motortemp <= 59 && totalefficiency > 0 && totalefficiency >= 60 && totalefficiency < 101 ) {
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
Serial.println("Nil flooding...");
lcd.setCursor( 0, 1);
lcd.print( "Nil flooding...");
delay(750);
Serial.println("Pump is OFF......");
lcd.setCursor( 0, 1);
lcd.print( "Pump is OFF......");
delay(750);
Serial.println("Motor NORMAL-ON.....");
lcd.setCursor( 0, 1);
lcd.print( "Motor NORMAL-ON.....");
delay(750);
}
else {
//IF ANY anomalous sensor states detected, return everything to normal operation state, note that pumps are disabled until the problem is found. Print the sensor statuses. Blink the masthead warning light.
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
delay(250);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
delay(250);
Serial.println("Error with sensors. Pumps on. All rc gear and motor off!");
Serial.println( "Main engine temperature: " );
Serial.println(motortemp);
Serial.println( "Cooling water intake temperature: " );
Serial.println(intaketemp);
Serial.println( "External water temperature: " );
Serial.println(externaltemp);
Serial.println( "ESC exhaust water temperature: " );
Serial.println(exhausttemp);
Serial.println( "Total cooling efficiency: " );
Serial.println(totalefficiency);
Serial.println( "Low water level sensor status: " );
Serial.println(lowfloodVal);
Serial.println( "Mid water level sensor status: " );
Serial.println(midfloodVal);
Serial.println( "High water level sensor status: " );
Serial.println(highfloodVal);
}
//Print Warning on the lCD
{ Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
lcd.setCursor( 0, 0);
lcd.print( " SENSOR/s ERROR......" );
//Print Motor temperature
lcd.setCursor( 0, 1);
lcd.print( "Motor C*: " );
lcd.setCursor( 11, 1);
lcd.print(motortemp);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
//Print ESC heat exchanger intake cooling water temperature, and then each sensor value thereafter, one per second
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " Intake C*: " );
lcd.setCursor( 12, 1);
lcd.print(intaketemp);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
//Print external water temperature
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " Extern. C*%: " );
lcd.setCursor( 14, 1);
lcd.print(externaltemp);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
//Print exhaust water temperature
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " Exhaus. C*%: " );
lcd.setCursor( 14, 1);
lcd.print(exhausttemp);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
//Print cooling loop efficiency
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " Total EFF%: " );
lcd.setCursor( 12, 1);
lcd.print(totalefficiency);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
//Print Low water level sensor HIGH 1 (no water detected)/LOW 0 (Detected) status
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " LOW Sens.: " );
lcd.setCursor( 12, 1);
lcd.print(lowfloodVal);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(24); // Send value to bank A
Wire.endTransmission();
//Print Mid water level sensor HIGH 1 (no water detected)/LOW 0 (Detected) status
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " MID Sens.: " );
lcd.setCursor( 12, 1);
lcd.print(midfloodVal);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
//Print High water level sensor HIGH 1 (no water detected)/LOW 0 (Detected) status
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " HIGH Sens.: " );
lcd.setCursor( 13, 1);
lcd.print(highfloodVal);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
//Print Bow flooding status
lcd.setCursor( 0, 1);
lcd.print( " " );
lcd.setCursor( 0, 1);
lcd.print( " BOW. Sens.: " );
lcd.setCursor( 13, 1);
lcd.print(bowfloodVal);
delay(2000);
Wire.beginTransmission(I2C_ADDR);
Wire.write(0x12); // Select bank A
Wire.write(8); // Send value to bank A
Wire.endTransmission();
//END OF LOOP
// MOTOR TEMPERATURE CONTROL ACTIVE IN THIS VERSION
}
}
}
}
}
}
}
//END OF ARDUINO MEGA SHIP-TECT 0.3b CODE!!!!!!!!
************************************************** ************************************************** ************************************************** ************************************************** ************************* ************************************************** ************************************************** ************************************************** ************************************************** ************************* Code:
//SLAVE UNO CODE (work in progress. Reads water level sensors and provides a single byte status to the Mega via I2C)
// Arduino Uno Wire Slave Sender for ShipTect 0.3b
// by Michael Judge 2013.
//Borrows some I2C sender code from the work of Nicholas Zambetti as it works perfectly :).
// <http://www.zambetti.com>
// This program at the current version, detects the water level from three sensors, low, mid and High, the same
// as the ship-tect mega version, and provides single byte value to the mega via
// I2C to indicate water level in the bow of the ship. This will be expanded
// to ballast tank water levels for multiple tanks in future versions.
#include <Wire.h>
void setup()
{
Serial.begin(14400);
Wire.begin(2); // join i2c bus with address #2
Wire.onRequest (floodCheck); // register event
//define flood detection pins
//configure pin2 as an input and enable the internal pull-up resistor
//HIGHFLOOD
pinMode(3, INPUT_PULLUP);
//MIDFLOOD
pinMode(2, INPUT_PULLUP);
//LOWFLOOD
pinMode(1, INPUT_PULLUP);
pinMode(13, OUTPUT);
digitalWrite(13, LOW);
}
void loop()
{
delay(100);
}
// function that executes whenever data is requested by master
// this function is registered as an event, see setup()
void floodCheck()
{
//Check sensors for flooding.
//Check for low-level flooding
int lowfloodVal = digitalRead(1);
Serial.println(lowfloodVal); Serial.print(" lowflood");
//Check for 2nd stage flooding.
int midfloodVal = digitalRead(2);
Serial.println(midfloodVal); Serial.print(" midflood");
//Check for high level flooding
int highfloodVal = digitalRead(3);
Serial.println(highfloodVal); Serial.print(" highflood");
// If third stage flooding detected, send the high flood value 3
if (lowfloodVal == LOW && midfloodVal == LOW && highfloodVal == LOW) {
Wire.write(3); // respond with message of 1 bytes
// as expected by master
Serial.println( "High flood sent" );
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
else {
// If 2nd stage flooding detected, send the mid flood value 2.
if (lowfloodVal == LOW && midfloodVal == LOW && highfloodVal == HIGH) {
Wire.write(2); // respond with message of 1 bytes
// as expected by master
Serial.println( "Medium flood sent" );
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
else {
// If 1st stage flooding detected, send the low flood value 1.
if (lowfloodVal == LOW && midfloodVal == HIGH && highfloodVal == HIGH) {
Wire.write(1); // respond with message of 1 bytes
// as expected by master
Serial.println( "Low flood sent" );
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
}
else {
//If no flooding detected. send the no flood value 0.
if (lowfloodVal == HIGH && midfloodVal == HIGH && highfloodVal == HIGH ) {
Wire.write(0); // respond with message of 1 bytes
// as expected by master
Serial.println( "None flood sent" );
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
else {
//If ERROR detected with sensor reads. Send the value 6, recognised by the mega as an error
// and flash the onboard D13 LED to indicate error.
Wire.write(6); // respond with message of 1 bytes
// as expected by master
Serial.println( "Error detected. error sent" );
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
delay(100);
digitalWrite(13, HIGH);
delay(100);
digitalWrite(13, LOW);
}
}
}
}
}
|
|
|
|
|
||
|
|
Quote:
Just take it in tiny, tiny bites - a subroutine at a time, and soon it'll start to fall together. Also, you really need to get hands on for this stuff. Get an (in this case) an Arduino and a copy of Getting Started with Arduino and Programming Arduino Getting Started with Sketches |
|
|
||
|
| Thread Tools | |
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Discussion DIY Telemetry System base on TI CC110L RF BoosterPack | spy004 | DIY Electronics | 2 | Mar 06, 2013 07:58 AM |
| Discussion Land Based FPV System (what's best)? | tsull | Video Piloting (FPV/RPV) | 12 | Feb 26, 2013 03:52 AM |
| Discussion Arduino Uno Based Flight Controller? | alexgator | Multirotor Electronics | 4 | Feb 15, 2013 12:49 AM |
| Discussion Arduino based ship safety (flood/overheat) system :D | Deckie | Scale Boats | 29 | Jan 27, 2013 05:27 AM |
| For Sale anti-collision flasher,NIP..$15. shipped | epoxyearl | Aircraft - General - Miscellaneous (FS/W) | 0 | Jan 04, 2013 04:32 AM |