Learning Lessons in code

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

First of all, what is wrong? Almost nobody here will have the same hardware configuration to play with. Are there any clues in this thread of yours that might shed some light on that? :slight_smile:

Whilst not inherently wrong, you have a number of functions defined as returning a boolean but they actually return nothing so could be declared void.

 topSensorState = digitalRead(topIRsensor);
  bottomSensorState = digitalRead(bottomIRsensor);

  if (topSensorState == 1 && bottomSensorState == 1) // if both sensors are unbroken

digitalRead() returns HIGH or LOW so it makes more sense to test the return value against HIGH or LOW rather than 1 although it will work as it is.

const int FeedValveOpen = 8;
const int FeedValveClose = 9;
const int FeedValveEnable = 10;

These could be declared as const byte to save memory.

bool topSensorState = 0;
bool bottomSensorState = 0;

A boolean variable can be either true or false. 0 may work but it is not actually correct.

so, instead of a motor i have two leds attached in place of :

const int FeedValveOpen = 8;
const int FeedValveClose = 9;

both ir detectors and emitters are working (i can see the IR through phone camera)

the led representing FeedValveClose is lit and serial monitor prints :

"Starting Feeder"
"Stopping Feeder"

just once and nothing more.

breaking the IR beams does nothing.

hope that helps.

If you're wondering what your sketch is doing, why don't you get it to tell you?
Use debug prints.

You never call the only functions that actually read the sensor inputs - FeedingProgress(), FeedRequired() and FeedNotRequired().

This:-if (bottomSensorState == 0 && digitalRead(FeedValveOpen == HIGH))
should be:-if (bottomSensorState == 0 && digitalRead(FeedValveOpen) == HIGH)
And you only call two of your functions. (But I guess you know that. :slight_smile: )
aarg also just mentioned it as I was typing.

OldSteve:
This:-if (bottomSensorState == 0 && digitalRead(FeedValveOpen == HIGH))
should be:-if (bottomSensorState == 0 && digitalRead(FeedValveOpen) == HIGH)
And you only call two of your functions. (But I guess you know that. :slight_smile: )
aarg also just mentioned it as I was typing.

Ah yes, first bit i missed.

I've changed that now, and called the functions in the loop.

Now in the serial monitor, i get "start feeder" scrolling with both beams unbroken, when i break the top sensor "start feeder" stops scrolling but the LED stays lit.

You haven't even begun to describe what's supposed to happen, and since you haven't shown us your code after you updated it, we can only guess. Accurate, descriptive comments throughout your code would go a long way, too.
You also need to show us your full schematic diagram to help us understand what's going on.

If you really want help you need to make it as easy as possible for people to help you.

I think that you need to rethink the logic to focus on inputs and outputs. The two pairs of sensors are your inputs, and the valve states are outputs.

I'm not sure that the state definitions and booleans are helping you think this through. As you said there are different ways of coding for the same result. Perhaps an input/output focus will help direct this program.

As aarg has said, you never read your sensors.

The loop should not be calling functions, but rather reading the sensors. The first two statements in loop should be

topSensorState = digitalRead(topIRsensor);
  bottomSensorState = digitalRead(bottomIRsensor);

The there will be a conditional statements of actions or function calls based on the four possible outcomes of top and bottom sensors.

blocked blocked > full do nothing
blocked open > error condition top blocked and bottom open or maybe seen when starting to fill?
open blocked > partially full do nothing
open open > empty fill feeder turn off when becomes blocked/ blocked

I think you only have to take action when the feeder is empty , and part of that action is to turn it off when the sensors say full. There are certainly issues of what the sensors read when the machine is filling and feed is moving past the sensors, but I'm not sure the extra boolean state "is filling" is the best way to do this.

I've gone right back to basics, to at least get one IR sensor working as it should.

I'm thinking if i control the rpm of the stepper motor, by the time the feed has fallen and broken the beam and the valve has closed there will be suficient in the hopper.

// Chicken Feeder to open and close valve controlled by 2 infra red sensors.
//

// declare the pin numbers to use
//the infrared sensors
const byte IRsensor = 11;
const byte IRled = 12;

//the Motor which rotates the feedvalve
const byte FeedValveOpen = 8;
const byte FeedValveClose = 9;
const byte FeedValveEnable = 10;

// the variables


int IRsensorState = 0;
int IRsensorLastState = 0;

void setup()
{
  pinMode(IRsensor, INPUT);
  digitalWrite(IRsensor, HIGH);
  pinMode(IRled, OUTPUT);
  pinMode(FeedValveOpen, OUTPUT);

  Serial.begin(9600);
}



void loop()
{
  IRsensorState = digitalRead(IRsensor);

  if (IRsensorState == HIGH)  {
    digitalWrite(IRled, HIGH);
    digitalWrite(FeedValveOpen, HIGH);

  }

  else {

    digitalWrite(IRled, LOW);
    digitalWrite(FeedValveOpen, LOW);
    digitalWrite(FeedValveClose, HIGH);

  }

  if (IRsensorState && !IRsensorLastState) {
    Serial.println("Empty");
  }

  if (!IRsensorState && IRsensorLastState) {
    Serial.println("Full");
  }
  IRsensorLastState = IRsensorState;
}

At least here the ir works, i can see it properly in the serial monitor.