SparkFun Forums 

Where electronics enthusiasts find answers.

General project discussion / help
Did you make a robotic coffee pot which implements HTCPCP and decafs unauthorized users? Show it off here!
By tb7wc0
#194964
Hi, I am trying to help my daughter (4th grade) build a small display for her teacher as a end of the school year gift. Her teacher has several short quotes and favorite words she always uses.
The ideal setup we would like to see will work as follows...
Plug it in, it will randomly display messages (messages DO NOT need to scroll) and automatically change messages after a predetermined time, (maybe 5 - 10sec). I found a sketch and did a little work (and mutilation) to it, I know there is some unnecessary stuff in there. Obviously if you glance at the code you will notice I have very, VERY little experience. Can someone, (or several someone's) give me some advice, places to look, or answers?

Thanks Bryan




#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const int switchPin = 6;

int switchState = 0;

int prevSwitchState = 0;
int reply;

void setup() {

Serial.begin(9600);
lcd.begin(16, 2);

pinMode(switchPin,INPUT);

lcd.print("Kyndra's");
lcd.setCursor(0, 1);
lcd.print("Message");
}

void loop() {

switchState = digitalRead(switchPin);
if (switchState != prevSwitchState) {

if (switchState == LOW) {
reply = random(14);
lcd.clear();

lcd.setCursor(0, 0);

switch(reply){
case 0 : lcd.print("Message 1"); break;
case 1 : lcd.print("Message 2"); break;
case 2 : lcd.print("Message 3"); break;
case 3 : lcd.print("Message 4"); break;
case 4 : lcd.print("Message 5"); break;
case 5 : lcd.print("Message 6"); break;
case 6 : lcd.print("Message 7"); break;

case 7 : lcd.print("Message 1");
lcd.setCursor(0, 1);
lcd.print("xyz"); break;
case 8 : lcd.print("Message 2");
lcd.setCursor(0, 1);
lcd.print("asd"); break;
case 9 : lcd.print("Message 3");
lcd.setCursor(0, 1);
lcd.print("dfd"); break;
case 10 : lcd.print("Message 4");
lcd.setCursor(0, 1);
lcd.print("dfdf"); break;
case 11 : lcd.print("Message 5");
lcd.setCursor(0, 1);
lcd.print("sample"); break;
case 12 : lcd.print("Message 6");
lcd.setCursor(0, 1);
lcd.print("again"); break;
case 13 : lcd.print("Message 7");
lcd.setCursor(0, 1);
lcd.print("keep trying"); break;

}
}
}

prevSwitchState = switchState;
}
By n1ist
#194974
Remove the test for the button and add a suitable delay between passes.
Code: Select all
void loop(void)
{
  reply = random(14);
  lcd.clear();

  lcd.setCursor(0, 0);

  switch(reply)
  {
    // Code to print a message here
  }

  // delay takes an argument in milliseconds - multiply by 1000 to get seconds.  This will delay 5 seconds
  delay (5000);
}
By tb7wc0
#194980
I reworked did a lot of work to this sketch and went a different direction. I need to fill and change messages yet, but my problem now is... I would like to push a button and have it cycle through displays by itself every couple 30sec to a minute. I have tried several approaches and found lots of ways it does not work. When I get it to cycle on its own, the random capabilities stop working and it displays everything in the order it is in the sketch. Can someone please get me on the right path?

Problems:
1. I want the program to display the intro message, until a button is pressed.
2. I would like each message to display for about 30 sec.
3. I would like both top and bottom display to be random

Right now I turn the Arduino on and the title message displays until the button is pressed, then a random message appears until I press the button again...

I believe once it advances on its own all it should need is a delay between messages.

Thanks
Bryan
Code: Select all
#include <LiquidCrystal.h>

#include <pgmspace.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const char cs1[] PROGMEM = "Be Kind";
const char cs2[] PROGMEM = "Think of others";
const char cs3[] PROGMEM = "Smile";
const char cs4[] PROGMEM = "Laugh";
const char cs5[] PROGMEM = "Love";
const char cs6[] PROGMEM = "Go for it!";
const char cs7[] PROGMEM = "Be a leader";
const char cs8[] PROGMEM = "GREAT JOB";
const char cs9[] PROGMEM = "Stay Focused";
const char cs10[] PROGMEM = "Keep it up!!!";

const char bl11[] PROGMEM = "Lucky number 7";
const char bl12[] PROGMEM = "Day will be great";
const char bl13[] PROGMEM = "test";
const char bl14[] PROGMEM = "random";
const char bl15[] PROGMEM = "";
const char bl16[] PROGMEM = "";


const char *str_tab[] = {
  cs1, cs2, cs3, cs4, cs5, cs6, cs7, cs8, cs9, cs10};
const int Number_of_Checksteps=10;

const char *bl_tab[] = {
  bl11, bl12, bl13, bl14, bl15, bl16};
  const int  Number_of_Kicks=6;

#define Longest_fort 100   

unsigned long count=0;
int run;
int buttonPin;     

void setup() {
buttonPin = 6;
  pinMode(buttonPin, INPUT_PULLUP);
  
  
  lcd.begin(16, 2);
 
  lcd.print("Mrs Bardonners");
  lcd.setCursor(0,1);

  lcd.print("4th grade class");
  delay (1000);
 
}

void loop() {
  if (BUTTON_PUSHED()) {
    lcd.clear();
    say_it();
  }
  while (BUTTON_PUSHED()) continue;
  delay(50);                        
  count++;
}

void say_it() {
  int i;
  char str[Longest_fort];  
  strcpy_P(str, str_tab[count % Number_of_Checksteps]);
  lcd.print(str);     
  lcd.setCursor(0,1);

char bl[Longest_fort];  
  strcpy_P(bl, bl_tab[count % Number_of_Kicks]);
  lcd.print(bl);     

}

int BUTTON_PUSHED() {
  if (digitalRead(buttonPin) == LOW) 
    return 1;                      
  return(0);                         
}
By n1ist
#194984
You don't have anything in there to do random selection. Also, you don't say when it stops.

I don't have an arduino handy to test this, but try something like this:
Code: Select all
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

const char cs1[] PROGMEM = "Be Kind";
const char cs2[] PROGMEM = "Think of others";
const char cs3[] PROGMEM = "Smile";
const char cs4[] PROGMEM = "Laugh";
const char cs5[] PROGMEM = "Love";
const char cs6[] PROGMEM = "Go for it!";
const char cs7[] PROGMEM = "Be a leader";
const char cs8[] PROGMEM = "GREAT JOB";
const char cs9[] PROGMEM = "Stay Focused";
const char cs10[] PROGMEM = "Keep it up!!!";

const char bl11[] PROGMEM = "Lucky number 7";
const char bl12[] PROGMEM = "Day will be great";
const char bl13[] PROGMEM = "test";
const char bl14[] PROGMEM = "random";
const char bl15[] PROGMEM = "";
const char bl16[] PROGMEM = "";


const char *str_tab[] = {
  cs1, cs2, cs3, cs4, cs5, cs6, cs7, cs8, cs9, cs10};

const char *bl_tab[] = {
  bl11, bl12, bl13, bl14, bl15, bl16};

#define BUTTON_PIN 6

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  
  lcd.begin(16, 2);
 
  lcd.print("Mrs Bardonners");
  lcd.setCursor(0,1);

  lcd.print("4th grade class");
  delay (1000);
 
  // Wait for button to be pressed
  while (!BUTTON_PUSHED());

  // Now go print messages
}

void loop() {
  char *ptr;

  // Get top line and print it
  ptr = str_tab[random(sizeof(str_tab)/sizeof(str_tab[0]))];
  lcd.setCursor(0,0);
  lcd_print_P(ptr);

  // Get second line and print it
  ptr = bl_tab[random(sizeof(bl_tab)/sizeof(bl_tab[0]))];
  lcd.setCursor(0,1);
  lcd_print_P(ptr);

  // Delay 30 seconds before printing next line
  delay (30000);
}


int BUTTON_PUSHED() {
  if (digitalRead(BUTTON_PIN) == LOW) 
    return 1;                      
  return(0);                         
}

void lcd_print_P(const PROGMEM char *s)
{
  uint8_t c;
  while ((c = pgm_read_byte_near(s++)) != 0)
     lcd.print(c);
}