well the codes I used >> regardless the LCD codes << since my real concern is only about the sensor and actuator
- First/
I assumed I want the sensor to send only one output > towards the one relay > using pin 13
<< when any object pass by the sensor it will active the relay for 2 sec
this is the code
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
//==========================================================================================
// avgFactor defines how many readings to average, more readings is slower but more accurate
//==========================================================================================
int avgFactor = 5;
int runningTotal;
int distRead;
int distAvg;
int i;
//==========================================================================================
// Define I/O pins:
//==========================================================================================
int actOUT1 = 13; // actuator1
int sharpIR1 = A1; // Sharp IRED input on Analog 1
//==========================================================================================
// Setup:
//==========================================================================================
void setup()
{
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Time");
lcd.setCursor(0, 1);
lcd.print("Items");
Serial.begin(9600);
lowACT1();
}
//==========================================================================================
// Main loop:
//==========================================================================================
void loop()
{
{
// set the cursor to column 5, line 0
lcd.setCursor(5, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
runningTotal = 0;
distRead = 0;
distAvg = 0;
getDist();
ACT1(distAvg);
}
//==========================================================================================
// Funtions:
//==========================================================================================
//==========================================================================================
// Get distance from Sharp IRED unit:
//==========================================================================================
int getDist()
{
for (i=0; i < avgFactor; i++)
{
distRead = analogRead(sharpIR1);
runningTotal += distRead;
// Diagnostic ouptut:
//Serial.print("i = ");
//Serial.println(i, DEC);
//Serial.print("runningTotal = ");
//Serial.println(runningTotal, DEC);
}
distAvg = (runningTotal / avgFactor);
// Diagnostic ouptut:
//Serial.print("distAvg = ");
//Serial.println(distAvg, DEC);
return distAvg;
}
//==========================================================================================
// Function to move the actuator in or out :
//==========================================================================================
int ACT1(int)
{
if (distAvg <= 100) //get the actuator out
{
lowACT1();
}
else if (distAvg > 100) //get the actuator in
{
highACT1();
delay(2000);
}
}
//==========================================================================================
// the actuator is moving
//==========================================================================================
int lowACT1()
{
analogWrite(actOUT1, 0);
}
//==========================================================================================
// the actuator is stopped
//==========================================================================================
int highACT1()
{
analogWrite(actOUT1, 255);
lcd.setCursor(9, 0);
lcd.print("Extend");
}
this code did work
- Second
I assumed I want the sensor to send only one output > towards the one relay > using pin 12
<< when any object pass by the sensor it will >>>> delay for 2 sec <<<< then active the relay for 2 sec
this is the code
#include <LiquidCrystal.h>
#include <Wire.h>
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Addr, En, Rw, Rs, d4, d5, d6, d7, backlighpin, polarity
//==========================================================================================
// avgFactor defines how many readings to average, more readings is slower but more accurate
//==========================================================================================
int avgFactor = 5;
int runningTotal;
int distRead;
int distAvg;
int i;
//==========================================================================================
// Define I/O pins:
//==========================================================================================
int actOUT2 = 12; // actuator2
int sharpIR2 = A2; // Sharp IRED input on Analog 2
//==========================================================================================
// Setup:
//==========================================================================================
void setup()
{
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print("Time");
lcd.setCursor(0, 1);
lcd.print("Items");
Serial.begin(9600);
lowACT2();
}
//==========================================================================================
// Main loop:
//==========================================================================================
void loop()
{
{
// set the cursor to column 5, line 0
lcd.setCursor(5, 0);
// print the number of seconds since reset:
lcd.print(millis()/1000);
}
runningTotal = 0;
distRead = 0;
distAvg = 0;
getDist();
ACT2(distAvg);
}
//==========================================================================================
// Funtions:
//==========================================================================================
//==========================================================================================
// Get distance from Sharp IRED unit:
//==========================================================================================
int getDist()
{
for (i=0; i < avgFactor; i++)
{
distRead = analogRead(sharpIR2);
runningTotal += distRead;
// Diagnostic ouptut:
//Serial.print("i = ");
//Serial.println(i, DEC);
//Serial.print("runningTotal = ");
//Serial.println(runningTotal, DEC);
}
distAvg = (runningTotal / avgFactor);
// Diagnostic ouptut:
//Serial.print("distAvg = ");
//Serial.println(distAvg, DEC);
return distAvg;
}
//==========================================================================================
// Function to move the actuator in or out :
//==========================================================================================
int ACT2(int)
{
if (distAvg <= 100) //get the actuator out
{
lowACT2();
}
else if (distAvg > 100) //get the actuator in
{
lowACT2();
delay(4000);
highACT2();
delay(2000);
}
}
//==========================================================================================
// the actuator is moving
//==========================================================================================
int lowACT2()
{
analogWrite(actOUT2, 0);
}
//==========================================================================================
// the actuator is stopped
//==========================================================================================
int highACT2()
{
analogWrite(actOUT2, 255);
lcd.setCursor(9, 0);
lcd.print("Shrink");
}
this code did work
Now >> conclusion << this is what I want
I want to combine the 2 codes using 2 outputs for 2 relays > so < when any object pass by the sensor
it will active only pin 13 for 2 sec
then it will active only pin 12 for 2 sec
in this way when only one relay active at a time >> the actuator will move only in one direction
but if they both were active this will make the actuator do nothing !!!
this is my problem I couldn't combine the 2 codes to do my requirement