# Learning Lessons in code

**URL:** <https://forum.arduino.cc/t/learning-lessons-in-code/381733>\
**Category:** Programming\
**Created:** [April 24, 2016, 11:34am UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733 "2016-04-24T11:34:07Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaverickAlex](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@MaverickAlex](https://forum.arduino.cc/u/MaverickAlex)\
**Post date:** [April 24, 2016, 11:34am UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/1 "2016-04-24T11:34:07Z")

</div>

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.

```cpp
/* 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

```

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [April 24, 2016, 11:36am UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/2 "2016-04-24T11:36:06Z")

</div>

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](http://forum.arduino.cc/index.php?topic=393897.0) that might shed some light on that? 🙂

---

<div class="post-metadata">

**Author:** ![UKHeliBob](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/ukhelibob/32/215663_2.png) [@UKHeliBob](https://forum.arduino.cc/u/UKHeliBob)\
**Post date:** [April 24, 2016, 11:54am UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/3 "2016-04-24T11:54:01Z")

</div>

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.

```cpp
 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.

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

```

These could be declared as const byte to save memory.

```cpp
bool topSensorState = 0;
bool bottomSensorState = 0;

```

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

---

<div class="post-metadata">

**Author:** ![MaverickAlex](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@MaverickAlex](https://forum.arduino.cc/u/MaverickAlex)\
**Post date:** [April 24, 2016, 11:57am UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/4 "2016-04-24T11:57:00Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [April 24, 2016, 12:00pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/5 "2016-04-24T12:00:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![anon57585045](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon57585045](https://forum.arduino.cc/u/anon57585045)\
**Post date:** [April 24, 2016, 12:14pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/6 "2016-04-24T12:14:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![OldSteve](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OldSteve](https://forum.arduino.cc/u/OldSteve)\
**Post date:** [April 24, 2016, 12:17pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/7 "2016-04-24T12:17:30Z")

</div>

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. 🙂 )  
aarg also just mentioned it as I was typing.

---

<div class="post-metadata">

**Author:** ![MaverickAlex](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@MaverickAlex](https://forum.arduino.cc/u/MaverickAlex)\
**Post date:** [April 24, 2016, 12:45pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/8 "2016-04-24T12:45:52Z")

</div>

> 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. 🙂 )  
> 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.

---

<div class="post-metadata">

**Author:** ![OldSteve](https://avatars.discourse-cdn.com/v4/letter/o/0ea827/32.png) [@OldSteve](https://forum.arduino.cc/u/OldSteve)\
**Post date:** [April 24, 2016, 1:06pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/9 "2016-04-24T13:06:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![cattledog](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/cattledog/32/217816_2.png) [@cattledog](https://forum.arduino.cc/u/cattledog)\
**Post date:** [April 24, 2016, 2:33pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/10 "2016-04-24T14:33:59Z")

</div>

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

```cpp
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.

---

<div class="post-metadata">

**Author:** ![MaverickAlex](https://avatars.discourse-cdn.com/v4/letter/m/67e7ee/32.png) [@MaverickAlex](https://forum.arduino.cc/u/MaverickAlex)\
**Post date:** [April 25, 2016, 3:45pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/11 "2016-04-25T15:45:45Z")

</div>

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.

```cpp
// 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.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 9:51pm UTC](https://forum.arduino.cc/t/learning-lessons-in-code/381733/12 "2021-05-05T21:51:39Z")

</div>

Closed on May 5, 2021, 9:51pm UTC


