0

Thank you for seeing my post.

I am trying to write a code that lets "A" displayed on row 1 and row 0 alternately on the LCD. starting from (0,1) then (1,0) then (2,1), then (3,0) etc.. when it reaches the last point is becomes "B" and bouncing back the other way until reaching the starting point when it becomes "A" again and repeat again..

I wrote this code but I got stuck at the end, I don't know how to bounce it back:


// include the library code:
#include <LiquidCrystal.h>

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

int x = 0;
int y = 1;

void setup()
{
    lcd.begin(16, 2);
    lcd.clear();
}

void loop()
{
    {
        {
            while (x < 15)
            {
                lcd.setCursor(x, y);
                lcd.print("A");
                delay(250);

                lcd.clear();
                x = x + 1;
                lcd.setCursor(x, y - 1);
                lcd.print("A");
                delay(250);

                lcd.clear();
                x + x++;
            }
        }
        {
            while (x = 15)
                lcd.setCursor(15, 0);
            lcd.print("B");
            delay(2000);
            x--;
        }
    }
}
  • Learn to use a for loop. It's much easier. And don't confuse assignment (=) with comparison (==) operators. – Majenko Nov 4 at 10:30
1

To me it seems, as if you have no knowledge about C/C++. ( I might be wrong. ;-) )

If you plan to do more programming projects, I advice you to learn the basics of this programming language before proceed with the projects. It would be hard thinking about an algorithm, if you don't know the tools to implement it.

In case you just need this once for a (let's say arts project [or homework ;-)] ), I add some example code. At the moment I have no chance to test the code. So it's blind written, and there might be typos. In about 8 hours I will have access to a test setup then I'll correct it if necessary.

#include <LiquidCrystal.h>

// Here you setup your LCD display
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);   

// you initialize the position where the next character 
// will be displayed (0,1) (x,y)
int x = 0;
int y = 1;
// setup is only done once
void setup()
{
    // initialize the lcd lib to be used with a 16x2 LCD
    lcd.begin(16, 2);
    // erase all character from the display
    lcd.clear();
}

// the loop is executed again and again and ...
void loop()
{
    // move forward
    // as long as the position is less than 15 
    // you allow x = 0, 1, 2, ... ,15 because
    // you increase two times in each loop step
    while (x < 15)
    {
        lcd.clear( );

        lcd.setCursor( x, y );
        lcd.print( "A" );
        delay( 250 );

        // step to the next position    
        x = x + 1;
        // toggle y
        y = toggle( y );

        lcd.clear();
        lcd.setCursor( x, y );
        lcd.print( "A" );
        delay( 250 );

        // step to the next position    
        x = x + 1;
        // toggle y
        y = toggle( y );
    }

    // now move backward
    // x is still 15 and you want to go backwards until x == 0
    while ( x > 0 )
    {
        lcd.clear( );

        lcd.setCursor( x, y );
        lcd.print( "B" );
        delay( 250 );

        // step to the next position    
        x = x - 1;
        // toggle y
        y = toggle( y );

        lcd.clear( );
        lcd.setCursor( x, y );
        lcd.print( "B" );
        delay( 250 );

        // step to the next position    
        x = x - 1;
        // toggle y
        y = toggle( y);
    }
}

// this is a subroutine that toggles y = 0 <-> 1
int toggle( int yval )
{
   // I use a ternary operator
   // in case y == 1, I return 0 and a 1 otherwise
   return ( yval == 1) ? 0 : 1;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.