Pages: [1] 2   Go Down
Author Topic: Programming Scrolling Question  (Read 982 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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);
}
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19366
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Code:
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.
Logged

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
« Last Edit: November 25, 2012, 02:59:50 am by doublea500 » Logged

Global Moderator
Melbourne, Australia
Offline Offline
Brattain Member
*****
Karma: 511
Posts: 19366
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Please post technical questions on the forum - not to me by personal message. Thanks a lot.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 654
Posts: 50931
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Do something like:

Code:
char shape;

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

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

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code still doesnt work.  not sure what the error is still smiley-sad.  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:
#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);
}
« Last Edit: November 25, 2012, 08:24:59 pm by doublea500 » Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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");
    }
   ...
}
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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;
Logged

Indiana, US
Offline Offline
Full Member
***
Karma: 12
Posts: 204
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You probably mean: delay(500);
Logged

There are 10 types of people in the world, those that understand binary, and those that don't.

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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);
}
Logged

Pages: [1] 2   Go Up
Jump to: