Hi all,
I've been having an annoying problem that I can't solve. It would seem like the solution is simple...but I've yet to find it. I am currently using a 3.3V Arduino Pro, hooked up to a Nokia LCD breakout and a stepper driver. I'm using two buttons on the LCD breakout to control the stepper. Button 1 advances the motor a single step, Button 2 a single revolution.
Now, I am updating the status on the LCD screen. So on startup it will read "Running void loop" On pressing button1 it will read "Ran single step" and with button2 it will read "Ran single rev".
The problem is that "Running void loop" shows up fine, but "Ran single step" shows up as "Ran single stepop" as it is shorter than the first line of text and those last two characters are left over. I've tried to somehow empty the string to remove them but nothing seems to work. I tried memset and a for loop and all this...it's driving me nuts! You can see all of my attempts commented out. Each one still does the same thing and shows "Ran single stepop".
Any advice?
// Stepper Driver Info
// Step Clock Pin: 7
// Half/Full Step Pin: 8
// Direction: 9
// Enable: 10
// Reset: 11
#include <NokiaLCD.h>
// Nokia LCD Setup settings
#define RED 0x1C
#define GREEN 0xE0
#define BLUE 0x03
#define YELLOW 0xFC
#define MAGENTA 0x1F
#define CYAN 0xE3
#define BLACK 0x00
#define WHITE 0xFF
NokiaLCD nokiaLcd;
int sensorPin = A0; // select the input pin for the potentiometer
int s1 = 12; // Switch 1 on Nokia LCD board
int s2 = 13; // Switch 2 on Nokia LCD board
int clockPin = 7;
int stepPin = 8;
int dirPin = 9;
int enablePin = 10;
int resetPin = 11;
int sensorValue = 0; // variable to store the value coming from the sensor
int switch1 = 0;
int switch2 = 0;
char text[50];
char text2[50];
char text3[50];
char pin1[6];
void setup()
{
DDRD |= B01111100; // Set SPI pins as output
PORTD |= B01111100; // Set SPI pins HIGH
text[0] = '\0';
text2[0] = '\0';
text3[0] = '\0';
nokiaLcd.lcd_init();
delay(100);
pinMode(s1, INPUT);
pinMode(s2, INPUT);
pinMode(clockPin, OUTPUT);
pinMode(stepPin, OUTPUT);
pinMode(dirPin, OUTPUT);
pinMode(enablePin, OUTPUT);
pinMode(resetPin, OUTPUT);
strcpy(text2,"Running void loop");
}
void loop()
{
strcpy(text3,"..........");
// read the value from the sensor:
sensorValue = analogRead(0);
strcpy(text,"ADC Value");
nokiaLcd.lcd_draw_text(BLUE, WHITE, 40, 20, text);
sprintf(pin1,"%i",sensorValue);
nokiaLcd.lcd_draw_text(RED, WHITE, 55, 37, pin1);
nokiaLcd.lcd_draw_text(BLACK, WHITE, 10, 55, text2);
// Stepper Motor connected to L297 Driver and Darlington Array.
// One step for each Switch 1 push, one revolution for each Switch 2 push.
digitalWrite(dirPin, HIGH);
digitalWrite(stepPin, HIGH);
switch1 = digitalRead(s1);
switch2 = digitalRead(s2);
//Loop 1
if (switch1 == LOW && switch2 == HIGH) {
digitalWrite(enablePin, HIGH);
digitalWrite(clockPin, HIGH);
delay(10);
digitalWrite(clockPin, LOW);
digitalWrite(enablePin, LOW);
delay(250);
//memset(text2,'\0',50);
for (int i=0;i<50;i++){
text2[i] = '\0';
}
//text2[0] = '\0';
strcpy(text2,"Ran single step");
}
//Loop2
if (switch2 == LOW && switch1 == HIGH) {
digitalWrite(enablePin, HIGH);
for (int i=0;i<200;i++) {
digitalWrite(clockPin, HIGH);
delay(5);
digitalWrite(clockPin, LOW);
delay(10);
}
strcpy(text2,"Ran single rev");
digitalWrite(enablePin, LOW);
}
//Loop3
if (switch2 == LOW && switch1 == LOW){
delay(500);
digitalWrite(enablePin, HIGH);
while (switch2 != LOW || switch1 != LOW){
digitalWrite(clockPin, HIGH);
delay(5);
digitalWrite(clockPin, LOW);
delay(20);
strcpy(text3,"Running continuous");
nokiaLcd.lcd_draw_text(BLACK, WHITE, 15, 70, text3);
}
digitalWrite(enablePin, LOW);
}
}