Simple increment question (for a display) - solved

Greetings,

I can print characters to an OLED display I have via screen (or the Arduino serial monitor) using the following code:
(I'm using this library. Google Code Archive - Long-term storage for Google Code Project Hosting.)

void loop() 
{
 int i = 0;
 char someChar[32] = {0};
 int row = 1;
 
 if(Serial.available())
 {
  do
  { someChar[i++] = Serial.read();
  } while (Serial.available());
  if (strcmp(someChar, "\r") != 0)
  {
   oled.write(someChar);
  }
  else oled.setCursor(0,row);
  row++;
 }
}

There's a method(?) I can use to change the cursor position on the OLED. With the way I have implemented it above, I can send a return carriage and the cursor will move down to the second row (row 1, the first row is 0). My problem is if I press the return key another time, rather than incrementally moving down another row like I want, the cursor resets to the second row (row 1) and the first column again. I think I'm not properly incrementing the row variable or it's getting reset each time the void loop repeats. Can you edit my code to produce the results I intend and explain where I went wrong?

Thanks!

Think through the do and if statements carefully.
Your program is reading one character, sending it to the display, and starting "void loop()" all over again.

So the reason it's not incrementing is because it's looping very fast after I read a character and write it to the screen, and then resetting the row variable to 1 right?
If that's the case, I just need help figuring out where to declare the row variable so that it increments properly.

Okay, so I declared it before the setup loop and now it increments, but sometimes it moves down more lines than expected. Seems to depend on timing or the number of characters I press before pressing return.

post your whole sketch that gives better means to debug the cause...
optionally make a minimal sketch that shows the problem. (often this already helps to find the bug)

jremington:
Think through the do and if statements carefully.
Your program is reading one character, sending it to the display, and starting "void loop()" all over again.

Really ? That do { } while() he has there, will read all the characters that are there. Which could be one, or more.

Thanks for the responses!
Here is my entire sketch:

// Simple Hello world demo

#include <SSD1306ASCII.h>

// pin definitions
#define OLED_DATA 2
#define OLED_CLK 3
#define OLED_DC 4
#define OLED_RST 5
#define OLED_CS 6

SSD1306ASCII oled(OLED_DATA, OLED_CLK, OLED_DC, OLED_RST, OLED_CS);
int row;
int column;

//------------------------------------------------------------------------------
void setup() 
{                

  // generate the high voltage from the 3.3v line internally
  oled.ssd1306_init(SSD1306_SWITCHCAPVCC);
  
  oled.clear();
  oled.setCursor(3,4);
  oled.print("Display link up!");
  delay(2000);
  oled.clear();
  oled.setCursor(0,0); 
  Serial.begin(19200);
}
//------------------------------------------------------------------------------
void loop() 
{
 int i = 0;
 char someChar[32] = {0};
 
 if(Serial.available())
 {
  do
  { someChar[i++] = Serial.read();
  } while (Serial.available());
  if (strcmp(someChar, "\r") != 0)
  {
   oled.write(someChar);
   column++;
  }
  if (column > 21)
   {
    column = 0;
    row++;
    oled.setCursor(column,row);
   }
  if (strcmp(someChar, "\r") == 0)
  {
  oled.setCursor(0,row);
  row++;
  column = 0;
  if (row > 8)
   {
    row = 0;
    row++;
    oled.clear();
   }
  }
 }
}

I now have this working (mostly) the way I wanted. The character only library I linked to above seems to be missing a built-in way to handle the return key, backspace key, text wrap, or resetting the screen when it is full of text. Since I need to incorporate these basic text editing features for a standalone device with its own keyboard, I realized I needed a way to track what position the cursor was at. I have a couple if () statements nested incorrectly in my loop (), and I'd like to go back and comment everything, but now I know why they call them nightly builds: I can deal the cleanup in the morning! :smiley:

Thanks again for looking at this.

 char someChar[32] = {0};
 int row = 1;
 
 if(Serial.available())
 {
  do
  { someChar[i++] = Serial.read();
  } while (Serial.available());

I don't suppose that it matters whether there is actually room in the array for the new character.

Actually, it matters a great deal.

  if (strcmp(someChar, "\r") != 0)

The someChar array is NOT NULL terminated, and, therefore, should NOT be passed to a function that expects a NULL terminated array of chars.