Go Down

Topic: Help with some programming!! (Read 2835 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.

PaulS

Just below this box, where you type messages, it says Additional Options...

One of them is Attach: with a name field and a Browse button. Browse to the file you want to attach (your sketch). Click Open. Then, type any text you want, and click Post, as normal. Your file will be attached.

kurtselva


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.


May I know what is this supposed to do?

And by "paste the output here", which output are you referring to?

What I did was I just put the Serial.begin code in the setup and the 3 sentences in the loop..

kurtselva

Also, may I know if there is any limit to the delay () function?

Because if I am going to do for 12 hr intervals, the millis would be a very large number : 43200000.

As long as it works, I don't mind the large number being there..

UKHeliBob



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.


May I know what is this supposed to do?

And by "paste the output here", which output are you referring to?

What I did was I just put the Serial.begin code in the setup and the 3 sentences in the loop..

One of the very few ways to debug code in the Arduino IDE is to print the value of variables to the Serial monitor at strategic points in the program so their value at that point can be seen.  That was what I was suggesting you do.  As to what output, I would have thought that it was obvious that I was referring to what you saw on the Serial monitor, but obviously I was not specific enough.
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

kurtselva

Ok, sry as I mentioned being new to arduino, I am not yet familiar with many codes

After running the programme with the serial codes, I get the output as :
p()
setCounter = 0
Starti
StaĆ²Starting loop()
setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 0

And this keeps on repeating..

UKHeliBob

No problem.

OK, so now we know that setCounter is zero each time through loop(), which is what we would expect if you have not set anything using the buttons, which I think you haven't.  The servo should not be moving because setCounter is not 7.  Can you confirm that it is not moving ?

If so, use the buttons to input values and get the servo moving.  You say that the movement occurs more than once.  Watch the value of setCounter just before it moves and just after.  It may help to put some more Serial.println()s in the servo() function so that you can see where it starts running in the Serial monitor window.

As before, paste some output here of the value of setCounter before and after the servo() function.

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

kurtselva

#26
Feb 11, 2013, 07:55 pm Last Edit: Feb 11, 2013, 07:59 pm by kurtselva Reason: 1
Ok. Actually I have made some changes to my programme (have attached it). Now the servo should rotate when setCursor is 5.  

I have added the serial codes to the servo () function as well and this a reduced version of what I got as output:

setCounter = 0
Starting loop()
setCounter = 0
Starting loop()
setCounter = 1
Starting loop()
setCounter = 1
Starting loop()
setCounter = 2
Starting loop()
setCounter = 2
Starting loop()
setCounter = 3
Starting loop()
setCounter = 3
Starting loop()
setCounter = 4
Starting loop()
setCounter = 4
Starting loop()
setCounter = 5
Starting loop()
setCounter = 5

The setCounter = 5 appears when the servo rotates, and I got this when it rotates at the required timing

But when the servo rotated randomly at unwanted timing, the output was still showing as setCounter = 0

To be more specific of my problem, the servo motor only rotates randomly when I have uploaded the programme onto the arduino and till the point when the cursor has been set to 5. After which, it rotates as needed

UKHeliBob

What meaning has setCounter == 5 got in your program ?

You are calling the servo() function from 8 places in your code.  That can't be right, can it ?  You only need to call it when the clock time equals the requested feed time.

I have just realised something.  You don't seem to have any form of clock in your program to compare the feeding times with, or have I missed something fundamental ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

PaulS

Perhaps you should add a Servo.write() before the Servo.attach() call, to direct the servo to a specific starting point. The servo should then not move until commanded to, in servo().

Code: [Select]
void servo()
{
  Serial.println("Starting loop()");
  Serial.print("setCounter = ");
  Serial.println(setCounter);

That first Serial.println() statement is crap.

kurtselva


Perhaps you should add a Servo.write() before the Servo.attach() call, to direct the servo to a specific starting point. The servo should then not move until commanded to, in servo().


Wow, it worked! Thanks alot!

Go Up