Go Down

Topic: calling a function repeatedly (Read 576 times) previous topic - next topic

alah

Feb 16, 2013, 06:28 pm Last Edit: Feb 16, 2013, 06:31 pm by AWOL Reason: 1
Hi  everybody;  I´m back again! Although I FINALLY wrote my first sketch,
It was almost frustrating. I couldn´t  implement " call ". Just for the sake of understanding, I´m posting my code, which works better than I thought. It´s very straightforward, my washer controller! I tried to declare a " function " and call it a few times:
4 times to WASH, Twice for first RINSE, Once for second RINSE, once again for third RINSE, then shutdown. Can you explain with some simple example. I also tried to post with # but " max characters exceeded"



Code: [Select]

       int start = 2;
       int fill = 3;
       int full = 4;
       int right = 5;
       int left = 6;
       int pump = 7;
       int spin = 8;
       int x = 0;

       void setup( ) {


       pinMode ( start, INPUT  );  // start  momentary switch
       pinMode ( fill, OUTPUT  );  // fill relay
       pinMode ( full,  INPUT  );  // full / empty sensor
       pinMode ( right, OUTPUT );  // motor right
       pinMode ( left, OUTPUT  );  // motor left
       pinMode ( pump, OUTPUT  );  // empty
       pinMode ( spin, OUTPUT  );  // centrifuge
       }
   
   
   
       void WASH ( ){
       
        /* wash 20 minutes; I´ll adjust delays and cycles accordingly.
           the difference in the condition of " x " in the FOR loops is
           what determines the time the cycle lasts; of course it won´t
           be 1 second. That was just for " debugging "; I won´t sit and
           watch these LEDs for 40min to see if it´s right */
     
        for ( int x = 0; x < 8; x ++ ){
       
        digitalWrite ( right, HIGH );
        delay(1000);
        digitalWrite ( right, LOW );
        delay(500);
        digitalWrite ( left, HIGH );
        delay(1000);
        digitalWrite ( left, LOW );
        delay(500);}
       
        digitalWrite( pump, HIGH ); // start pump
       
        do { // keep cycling while pumping
       
        digitalWrite ( right, HIGH );
        delay(1000);
        digitalWrite ( right, LOW );
        delay(500);
        digitalWrite ( left, HIGH );
        delay(1000);
        digitalWrite ( left, LOW );
        delay(500);}
        while ( digitalRead ( full ) == HIGH );
       
        if ( digitalRead ( full ) == LOW ){ // EMPTY ?
        digitalWrite ( pump, LOW );  //stop pump
        digitalWrite ( fill, HIGH );}  // open valve
       
        while ( digitalRead ( full ) == LOW ){
        digitalWrite (fill, HIGH );} // till full
       
        if ( digitalRead ( full ) == HIGH ){  // if full
        digitalWrite ( fill, LOW );}  //  close valve
                         
       
        for ( int x = 0; x < 4; x ++ ){ //  rinse ( wash 10 min )
       
        /*these delays are different only because I needed mysetup
        of LEDs indicate that the sketch was doing what I wanted.*/
       
        digitalWrite ( right, HIGH );  
        delay ( 3000 );
        digitalWrite ( right, LOW );
        delay(1000);
        digitalWrite ( left, HIGH );
        delay ( 3000 );
        digitalWrite ( left, LOW );
        delay ( 1000 );}
       
        digitalWrite ( pump, HIGH ); //  empty
       
        do {  //  while emptying,keep cycling
       
        digitalWrite ( right, HIGH );
        delay ( 3000 );
        digitalWrite ( right,LOW );
        delay ( 1000 );
        digitalWrite ( left, HIGH );
        delay ( 3000 );
        digitalWrite ( left, LOW );
        delay ( 1000 ); }
        while ( digitalRead ( full ) == HIGH ) ;
       
       
        if ( digitalRead ( full ) == LOW ) { // EMPTY ?
        digitalWrite ( pump, LOW );
        digitalWrite ( fill, HIGH ); }
       
        while ( digitalRead ( full ) == LOW ) {
        digitalWrite ( fill, HIGH ); }  
       
        if ( digitalRead ( full ) == HIGH ) {
        digitalWrite ( fill, LOW ); }
     
        for ( int x = 0; x < 2; x ++ ) {  // rinse ( wash 5 min )
       
        digitalWrite ( right, HIGH );
        delay ( 3000 );
        digitalWrite (right, LOW );
        delay ( 1000 );
        digitalWrite ( left, HIGH );
        delay ( 3000 );
        digitalWrite ( left, LOW );
        delay (1000 ); }
       
        digitalWrite (pump, HIGH );
       
        do {
       
        digitalWrite ( right, HIGH );
        delay ( 3000 );
        digitalWrite ( right, LOW );
        delay ( 1000 );
        digitalWrite ( left, HIGH );
        delay ( 3000 );
        digitalWrite( left, LOW );
        delay( 1000 );}
        while ( digitalRead ( full ) == HIGH );
       
       
        if ( digitalRead ( full ) == LOW ) { // EMPTY ?
        digitalWrite ( pump, LOW );
        digitalWrite ( fill, HIGH );
       
        }
       
        while ( digitalRead ( full ) == LOW ) {
        digitalWrite ( fill, HIGH ); } // OK
       
        if ( digitalRead ( full ) == HIGH ) {
        digitalWrite ( fill, LOW ); }
     
        for ( int x = 0; x < 2; x ++ ) { //  rinse ( wash 5 min )
       
        digitalWrite ( right, HIGH );
        delay ( 3000 );
        digitalWrite ( right, LOW );
        delay ( 1000 );
        digitalWrite ( left, HIGH );
        delay ( 3000 );
        digitalWrite ( left, LOW );
        delay ( 1000 ); }
       
        digitalWrite (pump, HIGH );
       
        do {
       
        digitalWrite ( right, HIGH);
        delay ( 3000 );
        digitalWrite ( right, LOW);
        delay ( 1000 );
        digitalWrite ( left, HIGH );
        delay ( 3000 );
        digitalWrite ( left, LOW );
        delay ( 1000 ); }
        while ( digitalRead ( full ) == HIGH );
       
       
        if ( digitalRead ( full ) == LOW ){ // EMPTY ?
        digitalWrite ( spin, HIGH );
        delay ( 5000 );  //stop eveything, WASHing´s done,now go and clean!
        digitalWrite ( pump, LOW );  
        digitalWrite ( spin, LOW );
       
       
        }}
       
        void loop( ) {
   
       
        if ( digitalRead ( start ) == HIGH ){
        digitalWrite ( fill, HIGH );}
        // When the " full " sensor goes HIGH,the valve closes
        if ( digitalRead ( full ) == HIGH ){    
        digitalWrite ( fill, LOW );
       
       
        WASH ( );
       
        }}



Moderator edit: CODE TAGS

PaulS

Can I suggest that your indentation is horrid. void loop() and void setup(), and all other functions, should not be indented at all.

Put the { on a new line.

Use Tools + Auto Format to correct the indenting issues.

One blank line between functions is good.

















Many is silly.

What is the problem? You have a function. You know how to call it.

alah

Hi PaulS, I DO accept any and every suggestion but PLEASE remember that this is my FIRST sketch.
I don´t even know what indentation , less tool + Auto Format means.
I  think that someone should forma forum ONLY forbeginners. :)

James C4S


I don´t even know what indentation , less tool + Auto Format means.

In the IDE go to the "Tools" menu and select "Auto Format".

I don't understand why you are having trouble calling a function more than once.

WASH();
WASH();
WASH();
WASH();

That would call your wash function 4 times.
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

spatula


I don´t even know what indentation , less tool + Auto Format means.


You should know by now. PaulS' advice is right for beginners, who might ignore that very few people will read their code line by line. Most people will do a fast scan of the code before zooming in to a smaller portion that attracts their attention, using visual clues such as the placement of instructions on the screen (spacing, empty lines). If it takes too much for people to understand your code they will loose interest and you won't get their help.

PaulS

Quote
I don´t even know what indentation , less tool + Auto Format means.

Look at this code:
Code: [Select]
void loop()
{
   if(moneyDeposited)
   {
      wash();
      rinse();
      dry();
   }
}

and compare it to this code:
Code: [Select]
void loop() {
if(moneyDeposited)
    {
              wash();
         rinse();
                          dry();
}}


Which looks better to you? To the compiler, they are the same code, but the structure of the program is easier to see when the { and } line up and the code inside the braces is consistently indented.

The Auto Format menu item, under the Tools menu item changes the second code to look more like the first code, except that it doesn't fix the { and }. Those you need to fix yourself.

Learning to use this tool early will help you with code structure from now on.

alah


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy