I've had a few posts on this forum now and have had mixed success with my coding skills.
I've learnt about using flags to display different messages on LCD's.
I've had mixed results getting my head around millis().
I've learnt the correct or at least the desired way of naming functions and mostly been successful with a bit of prodding at getting to where i need to be with my code. (simple things)
I have to say there is a diverse opinion on the way coding especially C++ should be written and lots of different ways to get to the same end result, which for a beginner is quite confusing.
I wrote in a previous post about not being able to see the wood for the trees and often browsing the different topics shows just how thick the forests can get, even amongst top coders.
I gave up on a recent project to use IR sensors on a project because i just couldnt get my head around certain aspects of it and found a simple way to "mechanically engineer" my way out of it.
Although, now i feel i should not just let it drop and i should learn where i'm going wrong in coding.
So my question is, i have started agin trying to rewrite the code so i can learn.
would someone look at my coding and offer constructive advice on where i'm doing things wrong.
/* Chicken Feeder to open and close valve controlled by 2 infra red sensors.
*/
// declare the pin numbers to use
//the infrared sensors
const int topIRsensor = 12;
const int bottomIRsensor = 13;
//the Motor which rotates the feedvalve
const int FeedValveOpen = 8;
const int FeedValveClose = 9;
const int FeedValveEnable = 10;
// the variables
bool topSensorState = 0;
bool bottomSensorState = 0;
// state of the feed
bool feederIsEmpty = true;
bool feederIsFull = true;
bool feederIsFilling = true;
// timing in millis not implemented as yet
//unsigned long previousMillis = 0;
//unsigned long Interval = 1000;
//unsigned long currentMillis = millis();
void setup()
{
Serial.begin(9600);
pinMode(topIRsensor, INPUT);
pinMode(bottomIRsensor, INPUT);
pinMode(FeedValveOpen, OUTPUT);
digitalWrite(FeedValveOpen, LOW);
pinMode(FeedValveClose, OUTPUT);
digitalWrite(FeedValveClose, LOW);
pinMode(FeedValveEnable, OUTPUT);
digitalWrite(FeedValveEnable, LOW);
}
// basic functions
//whats the state of the feed.
bool FeedRequired()
{
topSensorState = digitalRead(topIRsensor);
bottomSensorState = digitalRead(bottomIRsensor);
if (topSensorState == 1 && bottomSensorState == 1) // if both sensors are unbroken
{
feederIsEmpty = true;
feederIsFull = false;
feederIsFilling = false;
}
}
bool FeedNotRequired()
{
topSensorState = digitalRead(topIRsensor);
bottomSensorState = digitalRead(bottomIRsensor);
if (topSensorState == 0 && bottomSensorState == 0)
{
feederIsFull = true;
feederIsEmpty = false;
feederIsFilling = false;
}
}
bool FeedingProgress()
{
topSensorState = digitalRead(topIRsensor);
bottomSensorState = digitalRead(bottomIRsensor);
if (bottomSensorState == 0 && digitalRead(FeedValveOpen == HIGH))
{
feederIsFilling = true;
feederIsEmpty = false;
feederIsFull = false;
}
}
// feeding functions
void StartFeeding()
{
if (feederIsEmpty == true)
{
digitalWrite(FeedValveOpen, HIGH);
Serial.println("Starting Feeder");
}
}
void StopFeeding()
{
if (feederIsFilling == true && topSensorState == 0)
{
digitalWrite(FeedValveOpen, LOW);
digitalWrite(FeedValveClose, HIGH);
Serial.println(" Stopping Feeder ");
feederIsFull = true;
feederIsFilling = false;
feederIsEmpty = false;
}
}
void loop()
{
StartFeeding();
StopFeeding();
}
[/code/
Many Thanks
Alex