Go Down

Topic: Help with some programming!! (Read 2 times) previous topic - next topic

kurtselva

And I am new to arduino and this forum so sorry for those attachment/indentation etc etc issues

kurtselva

Guys, thanks for the suggestions. I have tried to add the setcounter to be 0 when then condition has been satisfied and it seems to work!

I still can't find the attachment icon so I have to post the code in 2 posts again :~
But I have made the auto indent
Code: [Select]
#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
int totalfeedforfeedinterval2 = 0;
int totalfeedforfeedinterval3 = 0;
int totalfeedforfeedinterval4 = 0;


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);
     }

     totalfeedforfeedinterval2 = numberofdays * 2;
     totalfeedforfeedinterval3 = numberofdays * 3;
     totalfeedforfeedinterval4 = numberofdays * 4;
   }

   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);
     }
   }

 


kurtselva

Code: [Select]
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);
      }
    }

    if (setCounter == 7)
    {
      if (feedinterval == 2)
      {
        for (int j = 0; j <= totalfeedforfeedinterval2; j++)
        {
          if (j == totalfeedforfeedinterval2)
          {
            setCounter = 0;
          }
          servo();
          delay (12000);
        }
      }
      else if (feedinterval == 3)
      {
        for (int j = 0; j <= totalfeedforfeedinterval3; j++)
        {
          if (j == totalfeedforfeedinterval3)
          {
            setCounter = 0;
          }
          servo();
          delay (8000);
        }
      }
      else if (feedinterval == 4)
      {
        for (int j = 0; j <= totalfeedforfeedinterval4; j++)
        {
          if (j == totalfeedforfeedinterval4)
          {
            setCounter = 0;
          }
          servo();
          delay (6000);
        }
      }
    }
  }
}

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
    }
  }
}


kurtselva

But I have another problem:

My servo motor starts to rotate randomly when the programme starts, it only rotates as needed when the setcursor is set to 7.

What I want is for the servo motor to ONLY rotate when the setcursor is set to 7. SO how should I eliminate the random rotations?

I am not very sure it if is connection problem or programming problem.

Other info: my servo motor is connected to pin 9 of arduino and the other two wires are connected to ground and a 6V external source.

UKHeliBob

#19
Feb 10, 2013, 07:59 pm Last Edit: Feb 10, 2013, 08:04 pm by UKHeliBob Reason: 1
Put
Code: [Select]
Serial.begin(9600);in setup() and
Code: [Select]
Serial.println("Starting loop()");
Serial.print("setCounter = ");
Serial.println(setCounter);

as the first 3 lines in loop() and paste the output here.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Go Up