PLEASE PLEASE HELP ME IM GOING NUTS!!!!

So Im new on the arduino world, and I think I have a ambitious project, I want to make a machine that its going to fill up a bucket with some quantity of food for horses, Each horse will have a code and each code will open a servo certain amount of time so the food quantity its the exact amount needed for each horse.

I have many problems the first its that I haven't been able to put different codes for the horses, I already made the code for one horse and its works pretty nice, but haven figure out how to increase the quantity of If and else statements for different times of servo opening.

I think that its easier if you guys take a look to the code and let me know.

But before this I would also like to input the code through a Number KEYPAD, I already have it working but only for printing on the serial screen not as the code input, but that would be for the future, firs I want to make the code working for several horses.

PLEASE PLEASE HELP ME.

#include <Servo.h>              // Servo Library

Servo myservo;                  // defines myservo as Servo

int inicialpostion = 0;             // Defines inicial postion of Servo
int fillingpos = 90;              // Defines postion where food will be served
int horsecode;                  // Defines horsecode
int timetrueno = 4000;          // Defines the time the servo would be open for Trueno Horse
int timeluna = 3500;            // Defines the time the servo would be open for Luna Horse
int horseconfirmation;          // Defines the horse name confirmation
int bucketconfirm;              // Defines the int that would confirm if the bucket its in place


void setup() {

Serial.begin(9600);           // Serial on
myservo.attach(12);           // Defines servo in pin 12
myservo.write(inicialpostion);    // Puts servo in postion 0

}

void loop() {
   
 Serial.println("Codigo del Caballo");      // We ask the horse code
 while (Serial.available()==0){}            // Wait for answer

horsecode = Serial.parseInt ();              // Read the horse code

 if(horsecode == 001){                                     // confirm that its correct
 Serial.println("Caballo Trueno ? 1 = Si 2 = No");         // Check horse name
 while (Serial.available()==0){}

horseconfirmation = Serial.parseInt();}                 // Read confirmation


 else {                                                // If the code its wrong prints error and goes back to the start of the loop.
 Serial.println("Error!!!");         
 Serial.println("Intente Nuevamente");
 delay(1500);
 return;
}

 if(horseconfirmation == 1){                           // If the horse its correct, prints confirmed.
 Serial.println ("Confirmado");
 delay(1500);}
 else {
 return;
}

Serial.println("Recipiente en su Lugar ? 1 = Si 2 = No");         // We ask to check if the bucket its in place 
 while (Serial.available()==0){}

bucketconfirm = Serial.parseInt();

  if (bucketconfirm == 1){                                     // If the answer its yes, then the bucket its filled up.
 Serial.println ("Confirmado");
 delay(1500);
 myservo.write(fillingpos);
 delay(timetrueno);
 myservo.write(inicialpostion);
 delay(2000);
} 
 else{                                                     // Goes back to start of the Loop.
   return; 
}
 return;                                                  // end of the code, goes back to the Start of the loop and waits for next horse.
}

If you want to have different settings for each of several horses you could store the settings in an array. For example

int horseMillis[5] = {0, 1500, 1700, 1300, 1900};

And when the user identifies a horse you can get the value with

fillTime = horseMillis[horseCode];

and, where you have delay(1500) you would use delay(fillTime)

This example assumes that the horses are numbered 1 to 4. Because arrays are numbered from 0 I have suggested a 5 element array in which element[0] is ignored. But you can change all that to suit yourself.

As a separate issue I suggest you use millis() rather than delay() to manage the time because the Arduino can do nothing else during a delay() - for example it can't check for a "STOP" button if the user should see the feed pouring onto the ground. The use of millis() is illustrated in several things at a time.

...R

raulandresmoch:
So Im new on the arduino world, and I think I have a ambitious project, I want to make a machine that its going to fill up a bucket with some quantity of food for horses, Each horse will have a code and each code will open a servo certain amount of time so the food quantity its the exact amount needed for each horse.

I have many problems the first its that I haven't been able to put different codes for the horses, I already made the code for one horse and its works pretty nice, but haven figure out how to increase the quantity of If and else statements for different times of servo opening.

I think that its easier if you guys take a look to the code and let me know.

But before this I would also like to input the code through a Number KEYPAD, I already have it working but only for printing on the serial screen not as the code input, but that would be for the future, firs I want to make the code working for several horses.

PLEASE PLEASE HELP ME.

Okay but take a step back and have another look.

You need to fill a bucket with the right amount of feed for any of a number of horses.
The bucket has to be in position before it is filled. That is why there are sensors, leds and buttons.

Will the horses change often? The horse data can be stored on-chip in the same space as your code (flash) and need to be re-programmed to change or it can be on an SD card as a text file you make/edit on your PC. Text from the PC either way can be used to save the user work and chance or mistypes.

Then you can have a board with horse names list and to the left of each name a led and a button. If the led is lit, the horse has already been fed. Else if button then fill the bucket.

No keypad needed, no mistyped numbers, just a button next to a led and name with data to match ready.

You make it so the data for each horse works the same in the one bucket fill function, then when a horse is chosen it is that one's data that gets used.

One block of code to watch buttons, one to run the leds, one the sensors, one the servo and one to process data from the inputs (buttons and sensors) and trigger the outputs (leds and servo).

All the time, every time through loop(), every piece runs even if just to check if it should do something.

They all work as separate entities greatly reducing complexity in each block, making them easier to write and maintain.

Even as the bucket is being filled, you can have a stop-right-now button that works. This is like the difference between playing chess and playing football. Chess is one piece at a time step-by-step. Football is everyone moves and coordinates in the real world by simple rules at the same time, which is way easier to do than it sounds.

WOW, thank you very much for the tips, I thought this would take a long time to be seen.

I will

Robin2:
If you want to have different settings for each of several horses you could store the settings in an array. For example

int horseMillis[5] = {0, 1500, 1700, 1300, 1900};

And when the user identifies a horse you can get the value with

fillTime = horseMillis[horseCode];

and, where you have delay(1500) you would use delay(fillTime)

This example assumes that the horses are numbered 1 to 4. Because arrays are numbered from 0 I have suggested a 5 element array in which element[0] is ignored. But you can change all that to suit yourself.

As a separate issue I suggest you use millis() rather than delay() to manage the time because the Arduino can do nothing else during a delay() - for example it can't check for a "STOP" button if the user should see the feed pouring onto the ground. The use of millis() is illustrated in several things at a time.

...R

Thank you so much for the help, I made a new code with your tip, but as soon as I put the number on the serial it moves the Servo, my idea was that the serial would ask first if the name of the horse was the correct one, so I tried to make an

int horseNames [5] = {horse1, horse2, horse3, horse4, horse5};

Obviously it didn't work LOL.

GoForSmoke:
Okay but take a step back and have another look.

You need to fill a bucket with the right amount of feed for any of a number of horses.
The bucket has to be in position before it is filled. That is why there are sensors, leds and buttons.

Will the horses change often? The horse data can be stored on-chip in the same space as your code (flash) and need to be re-programmed to change or it can be on an SD card as a text file you make/edit on your PC. Text from the PC either way can be used to save the user work and chance or mistypes.

Then you can have a board with horse names list and to the left of each name a led and a button. If the led is lit, the horse has already been fed. Else if button then fill the bucket.

No keypad needed, no mistyped numbers, just a button next to a led and name with data to match ready.

You make it so the data for each horse works the same in the one bucket fill function, then when a horse is chosen it is that one's data that gets used.

One block of code to watch buttons, one to run the leds, one the sensors, one the servo and one to process data from the inputs (buttons and sensors) and trigger the outputs (leds and servo).

All the time, every time through loop(), every piece runs even if just to check if it should do something.

They all work as separate entities greatly reducing complexity in each block, making them easier to write and maintain.

Even as the bucket is being filled, you can have a stop-right-now button that works. This is like the difference between playing chess and playing football. Chess is one piece at a time step-by-step. Football is everyone moves and coordinates in the real world by simple rules at the same time, which is way easier to do than it sounds.

Thank you very much for your HELP, I will take all your tips and try to implement them into my project, I will have some sensors for Humidity and Temperature, and also an alarm with some leds for the quantity of food left inside, Maybe with an GSM shield that could send an SMS to the food provider company etc. etc. Im really excited but its becoming complex and hard to do it.

Start with simple. Blink independent leds to get practice making things happen on time.

In my signature space below are 3 web addresses. The 1st two teach about time, serial i/o and simple state machine. With those you can get a lot out of Arduino.

I have done (fixed) GSM code to run reasonably smooth but the SIM900 library will hold everything up for over 30 seconds while connecting and no, it never had to that's just the marginal library. If your app can't be dumb-deaf-blind for a minute or less at a time then run the GSM on a dedicated controller and leave that for after the rest.

raulandresmoch:
I made a new code with your tip,

First rule of the Forum. If you want help you must post your new code. I can't guess what you did.

...R

...and the second rule is "You do not talk about the forum"

AWOL:
...and the second rule is "You do not talk about the forum"

? ? ?

Is that aimed at me ?
If so I don't understand ?

...R

Joke - { whoosh }

AWOL:
Joke - { whoosh }

Sorry - I was not familiar with that.

...R

Hi,

Reference Fight Club

Tom... :slight_smile:

raulandresmoch:
WOW, thank you very much for the tips, I thought this would take a long time to be seen.

I will
Thank you so much for the help, I made a new code with your tip, but as soon as I put the number on the serial it moves the Servo, my idea was that the serial would ask first if the name of the horse was the correct one, so I tried to make an

int horseNames [5] = {horse1, horse2, horse3, horse4, horse5};

Obviously it didn't work LOL.

delay(horse1) ;
cannot work. unless you assigned an integer to each value.
that is why he gave you numbers, integers, in the array.
take a minute to read the rules of the forum an learn how to put your code in code tags.
http://forum.arduino.cc/index.php/topic,148850.0.html
Rule F, 'you don't talk about this forum' is not listed as, well... you don't talk about this forum.

raulandresmoch:
int horseNames [5] = {horse1, horse2, horse3, horse4, horse5};

int is for 16-bit integers, numbers -32786 to +32767.

Horse name is text. Text in C is another whole category to learn how it works and what to do.

This is why I suggest a board with the names (chalk board may do) written or printed for humans by humans and for the system, a button and light and no messing around with text, display or keypad. The complicated stuff that takes extra code and allows more error to be handled, just cut that out if you can and have something that works sooner, maybe ever.

int is for 16-bit integers, numbers -32786 to +32767.

sp. int is for 16-bit integers, numbers -32768 to +32767.

An example of mistyping, why to avoid key entry where it can be avoided.