Pages: [1] 2 3   Go Down
Author Topic: Help with some programming!!  (Read 2303 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi guys,

I am doing a automatic feeder where the user inputs 3 information in a LCD display using 2 pushbuttons. and a food container will rotate at the specified timing using a servo motor.

The 3 info are: feeding interval per day, number of days to feed and time of 1st feed.

I have done the program to make the inputs increase to the required values for each info and am able to switch between the different info.

Now, I want to make the servo motor rotate at the specified timing interval when all the inputs have been inputted (i.e, the cursor is set to 7). From my code, the servo rotates at the specified interval but how do I make it stop after the specified days has been reached? By right, if the inputted feeding interval is 2, the interval will be every 12hr. But for testing purpose, I have made it to be 12sec interval. So what should I change my code so that, lets say the days to be fed is 2, then only 4 rotations of the motor at 12 sec interval is acheived and the motor does not rotate after that?

Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Below is my code: in 2 seperate posts due to word limit exceeded

Code:
#include <LiquidCrystal.h>                // Include libraries
#include <Servo.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);    // initialize the library with the numbers of the interface pins
Servo myservo;

const int  incrementPin = 6;              // the pin that the increment pushbutton is attached to
const int  setPin = 7;                    // the pin that the set pushbutton is attached to

int incrementPushButtonState = 0;                        // state of the increment pushbutton (low or high)
int setPushButtonState = 0;                              //  state of the set pushbutton
int setCounter = 0;                                      // no. of times set button is puched
int feedinterval = 1;                                    // feedinterval counter range 2-4
int numberofdays = 0;                                    // no. of days counter range 1-10
int firsthourvalue = 0;                                  // first hour value range 1-2
int secondhourvalue = 0;                                 // second hour value range 0-9
int firstminutevalue = 0;                                // first minute value range 1-5
int secondminutevalue = 0;                               // second minute value range 0-9
int servoposition = 0;                                   // position of servo

void setup()
 {
  pinMode(incrementPin, INPUT);    // initialize the button pin as a input:
  pinMode(setPin, INPUT);
  myservo.attach(9);               // attaches the servo on pin 9 to the servo object
  
  lcd.begin(20,4);                 // LCD has 20 columns, 4 rows
  lcd.setCursor(0,0);              // Choose column 0, row 0( 1st column, 1st row)
  lcd.print("Freq:     Days:");    // Print the stated
  lcd.setCursor(0, 1);             // Choose column 0, row 1
  lcd.print("1st feed time:");     // Print the stated
  lcd.setCursor (0, 2);            // Choose column 0, row 2
  lcd.print("current time:");      // Print the stated
  lcd.setCursor(0,3);              // Choose column 0, row 3
  lcd.print("");                   // Print the stated
  lcd.setCursor(16,1);             // Choose column 16, row 2
  lcd.print(":");                  // Print the stated
  
 }
 
 void loop()
 {
   {
   setPushButtonState = digitalRead(setPin);            // read the set pushbutton input pin:
     if (setPushButtonState == HIGH)                    // if the pushbutton is pressed, high
     {
       if (setCounter > 6)                              // if pushbutton pressed more than 6 times, rollover to 0
       {
         setCounter = 0;
       }
       setCounter++;                                    // counter increment by 1
       if (setCounter == 1)                             // if counter is 1,
       {
         lcd.setCursor(5,0);                             // set LCD to column 5, row 0 and
         lcd.blink();                                    // cursor blink
       }
        else
        if (setCounter == 2)                             // if counter is 2, blink cursor at stated position
        {
        lcd.setCursor(15,0);
        lcd.blink();
        }
       else
        if (setCounter == 3)                              // if counter is 3, blink cursor at stated position
        {
        lcd.setCursor(14,1);
        lcd.blink();
        }
        else
        if (setCounter == 4)                                // if counter is 4, blink cursor at stated position
        {
        lcd.setCursor(15,1);
        lcd.blink();
        }
        else
        if (setCounter == 5)                                  // if counter is 5, blink cursor at stated position
        {
        lcd.setCursor(17,1);
        lcd.blink();
        }
        else
        if (setCounter == 6)                                  // if counter is 6, blink cursor at stated position
        {
        lcd.setCursor(18,1);
        lcd.blink();
        }
        else
        if (setCounter == 7)                                  // if counter is 7, blinking stops
        {
        lcd.noBlink();
        }
      
       delay (1000);
     }
  
    
       if (setCounter == 1)                                        // if set counter is 1
       {
         incrementPushButtonState = digitalRead (incrementPin);    //read increment push button from increment pin
       if (incrementPushButtonState == HIGH)                       // if push button is pressed (high)
       {
         lcd.setCursor (5,0);                                     // set at 5th column, 0th row
         lcd.blink();
         feedinterval++;                                          // feed interval increases by 1
         lcd.setCursor (5,0);
         lcd.print(feedinterval);                                 // print value of freq
      
         if (feedinterval > 4)                                    // if freq value more than 4, reset to 2
         {
           feedinterval = 2;
           lcd.setCursor (5,0);
           lcd.print(feedinterval);
         }
         delay(300);
       }
       }
      
    
     else if (setCounter == 2)                                    // if set counter is 2
     {    
     incrementPushButtonState = digitalRead (incrementPin);      //perform same actions as for setCounter == 1
       if (incrementPushButtonState == HIGH)
       {
         lcd.setCursor (15,0);                                   // set at 15th column, 0th row
         lcd.blink();
         numberofdays++;
         lcd.setCursor (15,0);
         lcd.print(numberofdays);                                 // print value of freq
      
         if (numberofdays > 10)                                    // if freq value more than 10, reset to 1
         {
           lcd.setCursor (15,0);
           lcd.print ("  ");                                        // clears lcd character and specified position
           numberofdays = 1;
           lcd.setCursor (15,0);
           lcd.print(numberofdays);
          
         }
        
        
         delay(300);
       }
     }
 
« Last Edit: February 10, 2013, 10:40:03 am by kurtselva » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
else if (setCounter == 3)                                          // if set counter is 3
     {   
     incrementPushButtonState = digitalRead (incrementPin);          //perform same actions as for setCounter == 1
       if (incrementPushButtonState == HIGH)
       {
         lcd.setCursor (14,1);                                       // set at 14th column, 1st row
         lcd.blink();
         firsthourvalue++;
         lcd.setCursor (14,1);
         lcd.print(firsthourvalue);                                 // print value of freq
       
         if (firsthourvalue > 2)                                    // if freq value more than 2, reset to 1
         {
           firsthourvalue = 0;
           lcd.setCursor (14,1);
           lcd.print(firsthourvalue);
           
         }
         
         
         delay(300);
       }
     }
    else if (setCounter == 4)                                         // if set counter is 4
     {   
     incrementPushButtonState = digitalRead (incrementPin);          //perform same actions as for setCounter == 1
       if (incrementPushButtonState == HIGH)
       {
         lcd.setCursor (15,1);                                       // set at 15th column, 1st row
         lcd.blink();
         secondhourvalue++;
         lcd.setCursor (15,1);
         lcd.print(secondhourvalue);                                 // print value of freq
       
         if (secondhourvalue > 9)                                    // if freq value more than 9, reset to 0
         {
           secondhourvalue = 0;
           lcd.setCursor (15,1);
           lcd.print ("  ");
           lcd.setCursor (15,1);
           lcd.print(secondhourvalue);
           
         }
         
         
         delay(300);
       }
     }
   
     else if (setCounter == 5)                                           // if set counter is 5
     {   
     incrementPushButtonState = digitalRead (incrementPin);            //perform same actions as for setCounter == 1
       if (incrementPushButtonState == HIGH)
       {
         lcd.setCursor (17,1);                                         // set at 17th column, 1st row
         lcd.blink();
         firstminutevalue++;
         lcd.setCursor (17,1);
         lcd.print(firstminutevalue);                                 // print value of freq
       
         if (firstminutevalue > 5)                                    // if freq value more than 5, reset to 0
         {
           firstminutevalue = 0;
           lcd.setCursor (17,1);
           lcd.print(firstminutevalue);
           
         }
         
         delay(300);
       }
     }
     
     else if (setCounter == 6)                                           // if set counter is 6
     {   
     incrementPushButtonState = digitalRead (incrementPin);            //perform same actions as for setCounter == 1
       if (incrementPushButtonState == HIGH)
       {
         lcd.setCursor (18,1);                                          // set at 18th column, 1st row
         lcd.blink();
         secondminutevalue++;
         lcd.setCursor (18,1);
         lcd.print(secondminutevalue);                                 // print value of freq
       
         if (secondminutevalue > 9)                                    // if freq value more than 9, reset to 0
         {
           lcd.setCursor (18,1);
           lcd.print("  ");                                            // clears lcd character at specified position
           secondminutevalue = 0;
           lcd.setCursor (18,1);
           lcd.print(secondminutevalue);
           
         }
         
         delay(300);
       }
     }
     
     else if (setCounter == 7)
     {
     if (feedinterval == 2)
     {
     for (int j = 0; j <= numberofdays; j++)
     {
       servo();
       delay (12000);
     }
     }
     }
   }
 }
     
void servo()
{
  for (int i= 0; i < 1; i ++)
  {
  for(servoposition = 0; servoposition < 180; servoposition += 1)        // goes from 0 degrees to 180 degrees
  {                                                                      // in steps of 1 degree
    myservo.write(servoposition);                                        // tell servo to go to position in variable 'pos'
    delay(15);                                                           // waits 15ms for the servo to reach the position
  }
  for(servoposition = 180; servoposition>=1; servoposition-=1)           // goes from 180 degrees to 0 degrees
  {                               
    myservo.write(servoposition);                                        // tell servo to go to position in variable 'pos'
    delay(15);                                                           // waits 15ms for the servo to reach the position
  }
  }
}

 
       
         
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4238
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not answering your question, but when I was looking at your code I saw this
Code:
  for (int i= 0; i < 1; i ++)
How many times will this loop execute ?
How many times should it execute ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

It executes once: that is it goes from 0 degrees to 180 degrees and back to 0 degrees
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4238
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

So why the need for the loop at all ?
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hmm goood qn..

I copied it from the example sketch for servo  smiley-grin
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4238
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Now back to your question about stopping the servo movement after it has done it a specified number of times.
You know how many times it should move, so when you move the servo add 1 to a counter, but before you move the servo check that the total number of required moves has not already taken place.  If it has then don't move the servo.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, if you notice, I did just that in my code(See the last part where there is the int j)..
And I put the condition as j = numberofdays..
But it doesn't not work...And I can't identify the problem..
Code:
else if (setCounter == 7)
     {
     if (feedinterval == 2)
     {
     for (int j = 0; j <= numberofdays; j++)
     {
       servo();
       delay (12000);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But it doesn't not work...And I can't identify the problem..
Properly
indented
code
is
much
easier
to
read.

Complete
code
is
essential.

Describing
what
the
code
actually
does
is,
too.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Complete
code
is
essential.

Describing
what
the
code
actually
does
is,
too.

Look at my first 3 posts
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
       if (setCounter == 1)                                        // if set counter is 1
     else if (setCounter == 2) 
else if (setCounter == 3)                                          // if set counter is 3
    else if (setCounter == 4)                                         // if set counter is 4
     else if (setCounter == 5)                                           // if set counter is 5
     else if (setCounter == 6)                                           // if set counter is 6
     else if (setCounter == 7)
Wouldn't a switch statement make more sense? Wouldn't some functions help?

Why can't you use Tools + Auto Format to indent the code properly? Why didn't you attach the code instead of spreading t across multiple posts?

What exactly IS the code doing? How does that differ from what you want?
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 114
Posts: 4238
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

As Paul asked, you need to tell us what is not working.

The servo() function appears to be called once for each day that feeding is needed (numberofdays) and is called twice each day, but only if setCounter is 7
Code:
    else if (setCounter == 7)
    {
      if (feedinterval == 2)
      {
        for (int j = 0; j <= numberofdays; j++)
        {
          servo();
          delay (12000);
        }
      }
    }
This is in the loop() function so, if nothing happens to stop it the next time through loop() it will happen all over again.  This is the reason that I suggested you keep a count of how many times the servo had moved and stop it happening once the required number of moves has taken place.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49073
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
This is the reason that I suggested you keep a count of how many times the servo had moved and stop it happening once the required number of moves has taken place.
Alternatively, set setCounter back to 0 once the servo movement is done.
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 98
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok let me explain the code better:

user enters the following information using pushbuttons into a LCD display:
1) Feedinterval; this sets the interval between each feed and ranges from 2 -4. So if 2 is entered, then feed interval will be 12 hrly, 3 - 8hrly and 4 - 6 hrly
2) number of days for the feeding to continue. ranges from 1 to 10. SO if 2 is entered, then feeding will go one for 2 days etc
3) timefor first feed (this part will only come into play after I have worked on the existing problem)

So lets have a scenario: user inputs feedinterval= 2 and numberofdays = 2:
The servo will rotate a total of 2 times per day; so 4 times for 2 days..

For testing purpose, I cant wait for 12 hr interval. So I am testing with 12 sec interval instead.

SO the problem is I am not able to stop the servo from rotating even after the condition stated in the for loop has been satisfied..

Logged

Pages: [1] 2 3   Go Up
Jump to: