arduino+LCD +accelerometer+months stuck=plz hlp!!!

Hi Yalll,

i recently got my hands on an arduino 2009 (ATmel 328) board and a 128 * 64 lcd on ks 0108, despite the pin horrors i got the 2 to work just fine using library and sample code provided on arduino site for ks0108 library.

Now i wanted to add the accelerometer to the circuit. I got a 3 axis low g Accelerometer MMA7260 prototype PCB. This is where the trouble began.

(sureelectronics.net/goods.php?id=176)

I discovered that i need to use the analog inputs (A0-A5) so i can use the analogRead function, if you dealt with that library you know with regular pin assignment that’s not possible, since ks0108 is set up such that you A0-A4 for cs1 cs2 En Di and Rs.

After figuring out how to transfer mentioned pins to digital inputs 0 1 2, 12 and 13. I managed to free up the Analog input pins (A0-A5).

(Done by modifying ks0108.h file as so):

#define CSEL1            12            // CS1 Bit   // swap pin assignments with CSEL2 if left/right image is reversed

#define CSEL2            13            // CS2 Bit


#define R_W            1            // R/W Bit
#define D_I            0            // D/I Bit 
#define EN            2            // EN Bit
//#define RES             19            // Reset Bit  // uncomment this to contol LCD reset on this pin

Now connecting the accelerometer, pins and code were set as follows:
(note code used was GLCD example found arduino website with ks0108 library, and was modified only) same is true for modification above.

#include <ks0108.h>

int xr;   
int yr; 
int zr; 

const int s2 =14;
const int s1 =15;
const int xpin =16;
const int ypin =17;
const int zpin =18;
const int slp =19;

void setup(){
  
 pinMode(s2, OUTPUT);
 digitalWrite(s2, HIGH);
 pinMode(s1, OUTPUT);
 digitalWrite(s1, HIGH);
 pinMode(xpin, INPUT); 
 pinMode(ypin, INPUT); 
 pinMode(zpin, INPUT);
 pinMode(slp, OUTPUT);
 digitalWrite(slp, HIGH);

 
  GLCD.Init(NON_INVERTED);   
  GLCD.SelectFont(System5x7);  
  countdown(2); 
  GLCD.ClearScreen();
 
}


void  loop(){   
  

xr = analogRead(xpin) ;
yr = analogRead(ypin);
zr = analogRead(zpin);

  
  GLCD.ClearScreen();                
 GLCD.CursorTo(13,2);
  GLCD.Puts("X= ");
  GLCD.PrintNumber(xr);
  GLCD.CursorTo(13,3);
  GLCD.Puts("Y= ");
  GLCD.PrintNumber(yr);
  GLCD.CursorTo(13,4);
  GLCD.Puts("Z= ");
  GLCD.PrintNumber(zr);
 
  
}

my lcd screen with this code shows that both x and y values are 1023 while my z value fluctuates. i know its a pin assignment error in the end. but ive looked at every accelerometer example out there with no help.

if i switched the s2 or s1 to low the values of x or y would become 0 respectively and z would still fluctuate

sorry for the boring details, had t make the picture clear :smiley:

PS this is for a project and values must be displayed on LCD… and 2009 boards come with usb connection and not serial

I know much of this code is useless

So, cut it out, and concentrate on what your primary problem is. You'll almost certainly find that Serial works, so use that, and not some hacked LCD code.

Please go back to your post, click on "Modify" then highlight the code blocks and press the "#" (code) button on the editor's toolbar.

I'd look carefully at this section of code:

const int xpin =16;
const int ypin =17;
const int zpin =18;

I discovered that i need to use the analog outputs (A0-A5) so i can use the analogRead function, if you dealt with that library you know with regular pin assignment that's not possibl,

There is no analogue output on the arduino, you can use some pins as PWM outputs but don't confuse that with the analogue inputs A0 to A5.

sorry what i meant was inputs (A0-A5)..... its still the same question

PS> Issue corrected in initial post ;D as in the output input typo, not the situation............

Issue corrected in initial post

No, it’s still there.

const int xpin =16;
const int ypin =17;
const int zpin =18;

One thing i forgot to mention is that if i disconnected my accelerometer screen still shows same values....... i know, they seem to be driven by s1 and s2 being high or low ( but how? pin 16 reads 14 and 17 reads 15 thats the mystery).........which led me to conclusion of an internal pin assignment error ......hope that helps you help me lol

which led me to conclusion of an internal pin assignment error

No false conclusion.

Can you post a schematic of what you have as I am confused by your explanation. It could be that the input to the analogue pins is too high impedance to work correctly.

xr = analogRead(xpin) ; yr = analogRead(ypin); zr = analogRead(zpin);

That is wrong you don't use the digital pin values for an analogue input.

Look, write a new sketch, correct the analogue input pin assignments already pointed out, and just use simple Serial prints to display the values read from the "analogRead"s on the serial monitor.

About a fifteen line sketch.

ok.... the analog read is being read from pins 16-18 which are (A2-A4)....a Serial sketch might be easier, but this is for a project and has to be displayed on the lcd

the analog read is being read from pins 16-18 which are (A2-A4)....a Serial sketch might be easier,

First rule of debugging - KISS.

Build on things that you know work, and don't debug two broken things at the same time. When you're getting sensible results from your accelerometer, then plug that code back into your (by now debugged) LCD code.

Pins 16-18 are [u]digital[/u] pins.

thank youuuuuuu thats part of my confusion… then what are A0-A5 are refered to in code?

some examples were 012345 others 14-19 site suggeted A0-A5 but didnt work

PS how do you upload a sketch

The six analogue inputs are numbered 0..5. If you want to use them as digital I/O, then they're numbered 14..19. There's no confusion between analoue 0..5 and digital 0..5 because "analogRead" maps the pin for you, but it doesn't know about numbers 14..19.

I tried that

const int s2 =14;
const int s1 =15;
int xpin =2;
int ypin =3;
int zpin =4;
const int slp =19;

And now all values fluctuate regardless of connection as well

And now all values fluctuate regardless of connection as well

So now you are actually reading the analogue inputs what is connected to them. Try connecting a 10K pot to the analogue inputs and see if you get a steady reading.

PS how do you upload a sketch

You have already done this when you hit the upload button in the arduino interface.

Compile and run this, and post the results, please.

const int analogPin [] = {2, 3, 4};

void setup ()
{
  Serial.begin (9600);
}

void loop ()
{
  for (int i = 0; i < sizeof (analogPin) / sizeof (analogPin [0]); i++) {
    Serial.print (analogRead (i));
    Serial.print (" "); 
  }
  Serial.println ();
}
143 126 143 
148 136 128 
158 141 125 
152 152 131 
140 160 138 
128 152 152 
127 139 159 
137 126 155 
140 128 138 
150 132 128 
155 143 127 
152 152 128 
136 157 140 
126 153 150 
129 135 160 
134 127 151 
143 124 139 
148 135 127 
157 141 124 
149 151 131 
134 158 138 
128 149 153 
127 136 157 
135 124 152 
139 127 134 
152 132 125 
154 141 126 
149 153 128 
134 155 140 
124 150 151 
129 131 158 
134 125 149 
143 124 133 
149 134 127 
156 143 123 
148 152 131 
131 157 138 
126 146 154 
127 132 155 
135 123 149 
139 127 131 
152 134 123 
153 141 126 
146 154 129 
131 154 142 
123 148 152 
129 128 157 
134 125 147 
143 125 130 
149 133 126 
152 145 123 
145 152 132 
128 156 140 
127 143 156 
127 129 154 
138 122 145 
141 125 131

P.S. while not even connected to accelerometer

and while connected

334 320 338 
338 325 341 
340 328 336 
341 335 340 
334 339 336 
329 338 339 
326 328 340 
330 321 337 
334 322 341 
337 325 336 
343 330 340 
340 334 338 
335 340 338 
326 337 340 
325 326 336 
330 321 341 
332 319 337 
339 325 341 
341 329 341 
341 336 335 
335 341 340 
328 336 335 
328 328 341 
329 320 336 
334 322 338 
337 325 339 
343 330 336 
341 337 340 
332 339 336 
329 337 340 
326 325 338 
330 320 336 
332 320 341 
339 325 336 
343 331 341 
338 335 335 
333 340 340 
327 334 339 
328 325 338 
330 321 341 
332 320 336 
340 327 341 
341 330 336 
340 337 342 
331 339 341 
328 335 338 
328 326 341 
330 320 338 
336 323 340 
340 327 336 
343 332 338 
338 338 341 
331 339 335 
328 335 341 
327 323 336 
332 322 341 
335 322 338 
341 327 336 
341 332 340 
338 337 336 
332 340 341 
325 332 334 
328 324 341 
329 318 339 
336 322 338 
340 327 341 
340 331 336 
338 339 341 
329 337 336 
328 333 340 
327 323 340 
332 320 338 
337 324 341 
341 327 338 
343 334 341 
337 338 337 
330 338 335 
326 332 340 
329 322 335 
333 322 341 
335 323 335 
341 329 340 
341 333 339 
338 339 337 
328 338 340 
325 330 335 
328 322 341 
331 319 335 
337 323 341 
341 327 341 
342 333 337 
336 340 340 
328 337 335 
327 331 340 
327 320 336 
333 321 341 
335 323 340 
342 329 337 
342 335 340 
336 339 336 
329 338 341 
325 329 336 
329 321 337 
332 320 340 
339 324 336 
343 330 341 
340 334 335 
336 340 341 
327 337 338 
327 329 337 
328 321 340

Are you quite sure gravity is working where you are?

How have you got the range select pins on the accelerometer connected? And sleep mode?

;D that y we need that accelerometer..... yes thats why i was asking how to upload a picture someone asked for a schematic and i thought that was a good idea, but couldnt figure it out

C:\Users\M\Desktop\circuit.jpg

why are the values unstable and nonzero........ the 10k pot seemed to decrease values, but they still fluctuate or it kills to zero. and if so do i need 3 pots in this case

please checkout link on initial post for the accelerometer....... i connect the gnd and 5v from the board and manually set s1 s2 and sleep to high by connecting them to 5v also. and x y z go to A2-A4.... there is an EN clk txd rxd do and di on the accelerometer provider said that i2c is functionless so i thought the last 6 are useless...... did i miss anything

why are the values unstable and nonzero

The numbers for x and y would still be non-zero, because the zero point is offset to half the supply voltage. With the device horizontal on a stable surface, x and y should be around 1.65 volts, or about 340 counts. the value of z will then be higher or lower than these, depending on orientation.