GLCD Wii Tilt Code

I started a new thread from this post:

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1279128237/45#45

#include <glcd.h>
#include "fonts/Arial14.h"         // proportional font
#include "fonts/SystemFont5x7.h"   // system font
#include <Wire.h> // initialize wire
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx;

void setup()
{
  Serial.begin(19200);
  nunchuck_setpowerpins();
  nunchuck_init();
  GLCD.Init(NON_INVERTED);
  GLCD.ClearScreen();
  GLCD.SelectFont(System5x7);
  Serial.print("Wii nunchuck ready");
  GLCD.DrawHLine(0, 32, 127);

}

void loop()
{
   if( loop_cnt > 100 ) { 
        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); 

       Serial.println((byte)accx,DEC);
    
if ( accx > 180)
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 63, 127, 0);
}
else if ( accx > 130)
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0 , 32, 64, 0);
   }
else if ( accx < 130)
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
GLCD.DrawLine(0, 0, 127, 0);
   }
  
       
    }
    loop_cnt++;
    delay(1);
}

It's working but I need to get it fine tuned.
I prefer the math method as its easiest for me to use.
Based on tilt detection from a value of 73 to 185 I'm trying to make a tilt gauge.
I could use alot of the else if statements but thats going to be alot of code ;).
Whenever read 73 to 80 id implement a GLCD.DrawLine(0, 63, 127, 0)
from 80 to 85 id do a GLCD.DrawLine(0, 58, 127, 5) all the way till I get past a value of 130 and it'd go the oppposite way.
Edit:
New code working good

#include <glcd.h>
#include "fonts/Arial14.h"         // proportional font
#include "fonts/SystemFont5x7.h"   // system font
#include <Wire.h> // initialize wire
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx;

void setup()
{
  Serial.begin(19200);
  nunchuck_setpowerpins();
  nunchuck_init();
  GLCD.Init(NON_INVERTED);
  GLCD.ClearScreen();
  GLCD.SelectFont(System5x7);
  Serial.print("Wii nunchuck ready");
  GLCD.DrawHLine(0, 32, 127);

}

void loop()
{
   if( loop_cnt > 100 ) { 
        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); 

       Serial.println((byte)accx,DEC);
    
if ( accx > 180)
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 63, 127, 0);
}
else if(( accx < 179) && (accx > 170))
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0 , 59, 127, 5);
   }
else if ((accx < 169) && (accx > 160))
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
GLCD.DrawLine(0, 54, 127, 10);
}
else if((accx < 159) && (accx > 150))
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 49, 127, 15);
   }
  else if((accx < 149) && (accx > 140))
  {
      GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 45, 127, 20);
  }
  else if((accx < 139) && (accx > 130))
  {
      GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 41, 127, 25);
  }
  else if((accx < 129) && (accx > 120))
    {
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 32, 127, 32);
    }
    
       
    }
    loop_cnt++;
    delay(1);
}
#include <glcd.h>
#include "fonts/Arial14.h"         // proportional font
#include "fonts/SystemFont5x7.h"   // system font
#include <Wire.h> // initialize wire
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx;

void setup()
{
  Serial.begin(19200);
  nunchuck_setpowerpins();
  nunchuck_init();
  GLCD.Init(NON_INVERTED);
  GLCD.ClearScreen();
  GLCD.SelectFont(System5x7);
  Serial.print("Wii nunchuck ready");
  GLCD.DrawHLine(0, 32, 127);

}

void loop()
{
   if( loop_cnt > 100 ) { 
        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); 

       Serial.println((byte)accx,DEC);
    
if ((accx > 180) && (accx > 185))
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 0, 127, 63);

}
else if(( accx < 180) && (accx > 175))
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0 , 3, 127, 60);

   }
else if ((accx < 175) && (accx > 170))
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
GLCD.DrawLine(0, 6, 127, 57);

}
else if((accx < 170) && (accx > 165))
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 9, 127, 54);

   }
  else if((accx < 165) && (accx > 160))
  {
      GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 12, 127, 51);

  }
  else if((accx < 160) && (accx > 155))
  {
      GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 15, 127, 48);

  }
  else if((accx < 155) && (accx > 150))
    {
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 18, 127, 45);

    }
    else if((accx < 150) && (accx > 145))
    {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 21, 127, 42);

   }
   else if((accx < 145) && (accx > 140))
   {
      GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 24, 127, 39);

   }
   else if((accx < 140) && (accx > 135))
   {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 27, 127, 36);

   }
   
   else if((accx < 130) && (accx > 125))
   {
      GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 32, 127, 32);

   }
       else if((accx < 125) && (accx > 120))
       {
        GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 37, 127, 28);

       }
       else if((accx < 120) && (accx > 115))
       {
          GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 40, 127, 25);

       }
    else if((accx < 115) && (accx > 110))
     GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 43, 127, 22);

   }
   else if((accx < 110) && (accx > 105))
   {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 46, 127, 19);

   }
   else if((accx < 105) && (accx > 100))
   {
    
     GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 49, 127, 16);

   }
   else if((accx < 100) && (accx > 95))
    {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 52, 127, 13);

    }
    else if((accx < 95) && (accx > 90))
    {
       GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 55, 127, 10);

    }
    else if((accx < 90) && (accx > 85))
    {
       GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 58, 127, 7);

    }
    else if((accx < 85) && (accx > 80))
    {
      GLCD.ClearScreen();
      GLCD.DrawHLine(0, 32, 127);
      GLCD.DrawLine(0, 61, 127, 4);

    }
    else if ((accx < 80) && (accx > 75))
    {
       GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 63, 127, 0);

    }
    
    
      
    
    loop_cnt++;
    delay(1);
}

Works until tilted to the left and some garbled lines and the serial monitor freezes.
Looks like the data 75 to 125 hangs up. I took out 125 to 185 and does the same thing... :o
Shot a video:

You have some missing curly braces.
Amazingly it compiles and runs but the if statements are whacky
because of it and are not doing the logic you want.
You are missing one opening brace around this line:

    else if((accx < 115) && (accx > 110))
     GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 43, 127, 22);

   }

And the ending curly brace for the initial if on the

 if( loop_cnt > 100 ) {
        loop_cnt = 0;

which will be after all if/else and just before the loop_cnt++

Do an auto format in the IDE (click: Tools->autoformat ) to
adjust the indentation and you should quickly see the error.

The first range in your if statement is incorrect and will never evaluate to be true. You probably meant:

if ((accx > 180) && (accx < 185))

I notice that you accidentally created holes in your ranges. i.e. your ranges are not fully inclusive because one comparison is > and the next is < for the same value. This excludes the value.
For example, if the value is exactly 180 then neither of the two if statements across the 180 value boundary will trigger.
One of those will need to be a >= or <= to include the break point.

Also, since it looks like you are handling all the values with
no gaps, there is no need to fully check the range on each if.
You could do the if statements the way Michael had shown in his
example to avoid the >= or <= issue.

One thing you could do to really cut down on the code size is to move
the ClearScreen() and the DrawHline() outside the if/else statements
since all of them seem to be doing this.

--- bill

Assuming that you want to ignore sensor values that are not between 75 and 175 and you want the line height to vary linearly from 0 to 63 within that range, you can use the Arduino map function:

if( accx >= 75 && acx <= 185)
{
void y = map(accx, 75, 185, 0, 63); // map returns a value from 0 to 63 for values from 75 to 185
GLCD.DrawLine(127, 63-y, 0, y); // draw the line
}

You can also use the constrain function to ensure that the values are in the range 75 to 185.

Constrain would draw a line even if the values were outside the desired range.

The if statement only draws if the range is valid.

Use whichever is appropriate for your application.

Yes it does compile. Thanks guys for noticing. I was trying to get to if a value is greater than x but less than x do a glcd print. I'll rework it tonight and see what I get.
@bill
I'll look for the example.
I noticed in the examples you guys call a void () glcd clear.

This works surprisingly I have a tiny bit of flicker from 64,32 all the way to 127, 0 its not bad.

#include <glcd.h>
#include "fonts/Arial14.h"         // proportional font
#include "fonts/SystemFont5x7.h"   // system font
#include <Wire.h> // initialize wire
#include "nunchuck_funcs.h"

int loop_cnt=0;

byte accx;

void setup()
{
  Serial.begin(19200);
  nunchuck_setpowerpins();
  nunchuck_init();
  GLCD.Init(NON_INVERTED);
  GLCD.ClearScreen();
  GLCD.SelectFont(System5x7);
  Serial.print("Wii nunchuck ready");
  GLCD.DrawHLine(0, 32, 127);

}

void loop()
{
   if( loop_cnt > 100 ) 
        loop_cnt = 0;

        nunchuck_get_data();

        accx  = nunchuck_accelx(); 

       Serial.println((byte)accx,DEC);
    
if ((accx <= 185) && (accx > 180))
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 0, 127, 63);

}
else if(( accx < 180) && (accx >= 175))
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0 , 3, 127, 60);

   }
else if ((accx <= 175) && (accx > 170))
{
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
GLCD.DrawLine(0, 6, 127, 57);

}
else if((accx < 170) && (accx >= 165))
{
  GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 9, 127, 54);

   }
  else if((accx <= 165) && (accx > 160))
  {
      GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 12, 127, 51);

  }
  else if((accx < 160) && (accx >= 155))
  {
      GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 15, 127, 48);

  }
  else if((accx <= 155) && (accx > 150))
    {
GLCD.ClearScreen();
  GLCD.DrawHLine(0, 32, 127);
  GLCD.DrawLine(0, 18, 127, 45);

    }
    else if((accx < 150) && (accx >= 145))
    {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 21, 127, 42);

   }
   else if((accx <= 145) && (accx > 140))
   {
      GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 24, 127, 39);

   }
   else if((accx < 140) && (accx >= 135))
   {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 27, 127, 36);

   }
   
   else if((accx <= 135) && (accx > 130))
   {
      GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 30, 127, 33);
   }
    else if((accx < 130) && (accx >= 125))
    {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 32, 127, 32);

   }
       else if((accx <= 125) && (accx > 120))
       {
        GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 34, 127, 31);

       }
       else if((accx < 120) && (accx >= 115))
       {
          GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 37, 127, 28);

       }
    else if((accx <= 115) && (accx > 110))
    {
     GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 40, 127, 25);

   }
   else if((accx < 110) && (accx >= 105))
   {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 43, 127, 22);

   }
   else if((accx <= 105) && (accx > 100))
   {
    
     GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 46, 127, 19);

   }
   else if((accx < 100) && (accx >= 95))
    {
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 49, 127, 16);

    }
    else if((accx <= 95) && (accx > 90))
    {
       GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 52, 127, 13);

    }
    else if((accx < 90) && (accx >= 85))
    {
       GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 55, 127, 10);

    }
    else if((accx <= 85) && (accx > 80))
    {
      GLCD.ClearScreen();
      GLCD.DrawHLine(0, 32, 127);
      GLCD.DrawLine(0, 58, 127, 7);

    }
    else if ((accx < 80) && (accx >= 75))
    {
       GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 63, 127, 0);

    }
    
    
      
    
    loop_cnt++;
    delay(1);
}

I still left the draw line and clear screen in each else if.

I wonder if you could see any difference in the display using this simpler loop code that draws lines using linearly mapped spacing.

void loop()
{
  if( accx >= 75 && accx <= 185)
  {
    nunchuck_get_data();
    accx  = nunchuck_accelx();
    Serial.println((byte)accx,DEC);
    byte  y = map(accx, 75, 185, 0, 63); // map returns a value from 0 to 63 for values from 75 to 185 
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 63-y, 127, y);       // draw the line   
  }
  delay(100); // the time in milliseconds between redraws
}

I tried and I dont get anything from the display. Nothing in serial monitor. I took out the loop_cnt. Could be why it isn’t displaying anything.

Maybe I’ve got it coded wrong… I tried this:

#include <glcd.h>
#include "fonts/Arial14.h"         // proportional font
#include "fonts/SystemFont5x7.h"   // system font
#include <Wire.h> // initialize wire
#include "nunchuck_funcs.h"



byte accx;


void setup()
{
  Serial.begin(19200);
  nunchuck_setpowerpins();
  nunchuck_init();
  GLCD.Init(NON_INVERTED);
  GLCD.SelectFont(System5x7);

}

void loop()
{
  if( accx >= 75 && accx <= 185)
  {
    nunchuck_get_data();
    accx  = nunchuck_accelx();
    Serial.println((byte)accx,DEC);
    byte  y = map(accx, 75, 185, 0, 63); // map returns a value from 0 to 63 for values from 75 to 185
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 63-y, 127, y);       // draw the line
  }
  delay(100); // the time in milliseconds between redraws
}

if( accx >= 75 && accx <= 185)

I think I’d try reading “accx” before testing it.

The code I posted did not read the nunchuck data before checking the range. This should do what you want:

void loop()
{
  nunchuck_get_data();
  accx  = nunchuck_accelx(); 
  if( accx >= 75 && accx <= 185)
  {
    Serial.println((byte)accx,DEC);
    byte  y = map(accx, 75, 185, 0, 63); // map returns a value from 0 to 63 for values from 75 to 185
    GLCD.ClearScreen();
    GLCD.DrawHLine(0, 32, 127);
    GLCD.DrawLine(0, 63-y, 127, y);       // draw the line
  }
  delay(100); // the time in milliseconds between redraws  
}

@mem
No more flickering :wink:
The DrawLine isn't even at at the center of the Horizontal line.