Programming Scrolling Question

So i am a beginner at both c++ and arduino so im just trying to get a small menu going that has a star, triangle and a square(premade) here is what i have so far. I am going off a few of the examples but changed them. So far what i have is that it goes to the last if statement, in this case, the one where the shape == 2 and does that without doing anything else. It is not scrolling/(fake scrolling...). it isnt detecting which shape is which. im pretty sure something is wrong with my syntax. try to ignore the comments, i had to comment out quite a bit to save for later.

#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
/*
 * OrangutanPushbuttonExample: for the 3pi robot, Orangutan LV-168,
 *    and Orangutan SV-xx8
 *
 * This example uses the OrangutanPushbuttons library to detect user input
 * from the pushbuttons, and it uses the OrangutanLCD library to display
 * feedback on the LCD.
 *
 * http://www.pololu.com/docs/0J17/5.f
 * http://www.pololu.com
 * http://forum.pololu.com
 */

OrangutanPushbuttons buttons;
OrangutanLCD lcd;

const char Triangle[] PROGMEM = {
  0b00000,                 // the five bits that make up the top row of the 5x8 character
  0b00000,
  0b00100,
  0b00100,
  0b01110,
  0b11111,
  0b11111,
  0b00000
};

const char Square[] PROGMEM = {
  0b00000, 
  0b00000,
  0b11111,
  0b10001,
  0b10001,
  0b10001,
  0b11111,
  0b00000
};

const char Star[] PROGMEM = {
  0b00000, 
  0b10101,
  0b01010,
  0b10001,
  0b01010,
  0b10101,
  0b00000,
  0b00000
  
};


void setup()                    // run once, when the sketch starts
{
  
  lcd.loadCustomCharacter(Triangle, 0);
  lcd.loadCustomCharacter(Square, 1);
  lcd.loadCustomCharacter(Star, 2);
  lcd.clear();
  char shape;
  shape = 0;
  lcd.print(shape);
  //lcd.print(Star);
}
void loop()                     // run over and over again
{
  
  
  
  // wait for either the top or bottom buttons to be pressed
  // store the value of the pressed button in the variable 'button'
 // unsigned char button = buttons.waitForPress(TOP_BUTTON | BOTTOM_BUTTON | MIDDLE_BUTTON);
 unsigned char button = buttons.waitForPress(TOP_BUTTON);
  //lcd.clear();
  
 // while (button != MIDDLE_BUTTON){  
    char shape = 0;
    lcd.print(shape);
 
   if ((button == TOP_BUTTON) && (shape == 0))     // display the button that was pressed
    {shape = 1;
    lcd.clear();
    lcd.print(shape);
    }
    
   
   if ((button == TOP_BUTTON) && (shape == 1))     // display the button that was pressed
    {
      shape = 2;
    lcd.clear();
    lcd.print(shape);
    }
    
   if ((button == TOP_BUTTON) && (shape == 2))     // display the button that was pressed
    {shape = 0;
    lcd.clear();
    lcd.print(shape);
    }
    
  
 
  //}
 // if (button == MIDDLE_BUTTON && Shpe == 1)
 //   lcd.print("middle down");
  // if (button == BOTTOM_BUTTON)
  //  lcd.print("bot down");

//  delay(1000);
}
void loop()                     // run over and over again

{
...
  char shape = 0;

...

if ((button == TOP_BUTTON) && (shape == 0))    // display the button that was pressed

shape will always be zero every time through loop, because you just made it zero.

I suggest moving the declaration for shape outside loop.

two things now. I got rid of the char shape = 0; line in the loop and still have it in the setup. gets the same thing done though, not sure where to put it. Second of all, even if it is always 0, it is not detecting that it is 0. for some reason it is always the last if statement, which in this case is the shape==2 for some reason. could it be something having to do with it being a local and not global variable? if so, how do i change that?

The "shape" in setup is a different variable to "shape" in loop. You have to move it outside either function.

so basically just do char shape; outside everything and it will work?

so basically just do char shape; outside everything and it will work?

Define the shape variable as a global, and test. Let us know what happens.

how do i define it as global :)... im trying to do public: but its nots working

Only declare shape once, and put it outside setup() and loop(). Otherwise it will only be available to them.

Do something like:

char shape;

void setup()
{
  shape = 0;
  ...
}

void loop()
{
  use shape here...
}

Code still doesnt work. not sure what the error is still :(. here it is though.

what happens- it goes to the triangle first, then i click the top button and it goes to "shape3" when it should be "shape1"

#include <OrangutanLCD.h>
#include <OrangutanPushbuttons.h>
/*
 * OrangutanPushbuttonExample: for the 3pi robot, Orangutan LV-168,
 *    and Orangutan SV-xx8
 *
 * This example uses the OrangutanPushbuttons library to detect user input
 * from the pushbuttons, and it uses the OrangutanLCD library to display
 * feedback on the LCD.
 *
 * http://www.pololu.com/docs/0J17/5.f
 * http://www.pololu.com
 * http://forum.pololu.com
 */

OrangutanPushbuttons buttons;
OrangutanLCD lcd;

const char Triangle[] PROGMEM = {
  0b00000,                 // the five bits that make up the top row of the 5x8 character
  0b00000,
  0b00100,
  0b00100,
  0b01110,
  0b11111,
  0b11111,
  0b00000
};

const char Square[] PROGMEM = {
  0b00000, 
  0b00000,
  0b11111,
  0b10001,
  0b10001,
  0b10001,
  0b11111,
  0b00000
};

const char Star[] PROGMEM = {
  0b00000, 
  0b10101,
  0b01010,
  0b10001,
  0b01010,
  0b10101,
  0b00000,
  0b00000
  
};

char shape;

void setup()                    // run once, when the sketch starts
{
  
  lcd.loadCustomCharacter(Triangle, 0);
  lcd.loadCustomCharacter(Square, 1);
  lcd.loadCustomCharacter(Star, 2);
  lcd.clear();
  shape = 0;
  lcd.print(shape);
  //lcd.print(Star);
}


void loop()                     // run over and over again
{
  
  
  
  // wait for either the top or bottom buttons to be pressed
  // store the value of the pressed button in the variable 'button'
 // unsigned char button = buttons.waitForPress(TOP_BUTTON | BOTTOM_BUTTON | MIDDLE_BUTTON);
 unsigned char button = buttons.waitForPress(TOP_BUTTON);
  //lcd.clear();
  
 // while (button != MIDDLE_BUTTON){
 
 
 
   if ((button == TOP_BUTTON) && (shape == 0))     // display the button that was pressed
    {shape = 1;
    lcd.clear();
    lcd.print("shape1");
    }
    
   
   if ((button == TOP_BUTTON) && (shape == 1))     // display the button that was pressed
    {
      shape = 2;
    lcd.clear();
    lcd.print("shape2");
    }

  if ((button == TOP_BUTTON) && (shape == 2))     // display the button that was pressed
    {shape = 0;
    lcd.clear();
    lcd.print("shape3");
    }
  
  
 
  //}
 // if (button == MIDDLE_BUTTON && Shpe == 1)
 //   lcd.print("middle down");
  // if (button == BOTTOM_BUTTON)
  //  lcd.print("bot down");

//  delay(1000);
}

Got it... It's doing exactly what you told it to do!

You might want to make them else ifs instead of sequential ifs. When you change shape to 1 in the first if statement block, it triggers the logic for the second, which triggers the logic for the third (shape3)...

Try something like this:

void loop()                     // run over and over again
{
  ...

   if ((button == TOP_BUTTON) && (shape == 0))     // display the button that was pressed
    {shape = 1;
    lcd.clear();
    lcd.print("shape1");
    }
   
  else if ((button == TOP_BUTTON) && (shape == 1))     // display the button that was pressed
    {
      shape = 2;
    lcd.clear();
    lcd.print("shape2");
    }

  else if ((button == TOP_BUTTON) && (shape == 2))     // display the button that was pressed
    {shape = 0;
    lcd.clear();
    lcd.print("shape3");
    }
   ...
}

sweet thanks that works, i even made it so it works for the bottom button!

Great! Good luck with your project. LCDs are fun, but can be a challenge. Enjoy.

eh now i just feel like a moron... why is this not making both motors move for .5 seconds then only 1 on for a second? whats happening is that both are on and dont turn off...

int motorPin1 = 5;
int motorPin2 = 11;
void setup(){
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
}
void loop(){
  digitalWrite (motorPin1, HIGH);
  digitalWrite (motorPin2, HIGH);
 delay ;500;
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, HIGH);
delay ;1000;

You probably mean: delay(500);

yea thats right that looked very funky. now onto the next question... my first legitimate question!

trying to run the code to test it out. the second i put the 3pi robot on the ground it powers off, it works just fine while the wheels are not touched though.

int motorPin1 = 5;
int motorPin2 = 3;
void setup(){
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
}
void loop(){
  delay(500);
  digitalWrite (motorPin1, HIGH);
  digitalWrite (motorPin2, HIGH);
 delay (500);
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, HIGH);
delay (500);
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, LOW);
delay (500);
}

How are you powering the motors? One common mistake is to power them from the Arduino board. They'll need their own power supply or battery pack (always connect the grounds together).

It sounds like you might be drawing too much power when you put load on the motors. This makes the Arduino reboot.

Next question (low batteries was the answer to the last one sorry!)

my only thing is that the code is correct but the system doesnt follow it perfectly. the burst is too high on the wheels (they accelerate too fast too quickly in a way that when it supposed to move straight, it just doesnt...) how do i lower the speed of the wheels?

object: make motors into a square
current code:

int motorPin1 = 6;
int motorPin2 = 11;
void setup(){
pinMode (motorPin1, OUTPUT);
pinMode (motorPin2, OUTPUT);
}
void loop(){
  delay (700);
  digitalWrite (motorPin1, HIGH);
  digitalWrite (motorPin2, HIGH);
  delay (100);
  digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, LOW);
delay(700);
 
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, HIGH);
delay(45);
digitalWrite (motorPin1, LOW);
digitalWrite (motorPin2, LOW);
}

Without knowing how your motors are wired up, I really can't say. I'm curious, How are they configured, how are you controlling them (outputs driving transistors? a shield?), and how are you powering them (separate batteries?)?

From looking at the code, you are turning them on and off digitally -- full speed on / complete stop.

Alternatively, you can drive them using pulse-width modulation (PWM) and output a lower "voltage" to slow them down. If you are not sure how, do a quick google on "Arduino PWM" to get what you need. Its fairly easy to do.

Pat.