Go Down

Topic: Programming Scrolling Question (Read 1 time) previous topic - next topic

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.

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

Nick Gammon

Quote
Code: [Select]
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.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

#2
Nov 25, 2012, 08:57 am Last Edit: Nov 25, 2012, 08:59 am by doublea500 Reason: 1
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?

Nick Gammon

The "shape" in setup is a different variable to "shape" in loop. You have to move it outside either function.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

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

PaulS

Quote
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

patduino

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

Do something like:

Code: [Select]

char shape;

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

void loop()
{
  use shape here...
}
There are 10 types of people in the world, those that understand binary, and those that don't.

#8
Nov 26, 2012, 02:23 am Last Edit: Nov 26, 2012, 02:24 am by doublea500 Reason: 1
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"

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

patduino

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:

Code: [Select]

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");
    }
   ...
}
There are 10 types of people in the world, those that understand binary, and those that don't.

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

patduino

Great!  Good luck with your project.  LCDs are fun, but can be a challenge.  Enjoy.
There are 10 types of people in the world, those that understand binary, and those that don't.

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


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

patduino

You probably mean: delay(500);
There are 10 types of people in the world, those that understand binary, and those that don't.

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.



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

Go Up