Hi! Still having trouble getting this LCD working, have 2 push buttons that supposed to operate 1 led on/off and display different messages on the screen.
I've been trying to assign both functions to the push-buttons, without any luck. Now I tried to assign the led to the push-buttons and the LCD to the led.
With this code, even the LED won't work.
//lcd
#include "U8glib.h"
// A linha abaixo define as ligacoes e deve ser
// ajustada conforme o display utilizado.
U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 ,7); //Enable, RW, RS, RESET
int display = 1;
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 9; // the number of the pushbutton pin 2
const int buttonPin2 = 10; // the number of the pushbutton pin 3
int ledPin = 12; // the number of the LED pin
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
int ledState = 0;
void u8g_prepare()
{
u8g.setFont(u8g_font_6x10);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
}
void u8g_Screen1() //Screen 1 - RECORDING IN PROGRESS
{
u8g.setFont(u8g_font_9x18B);
u8g.drawStr( 5, 28, "RECORDING IN");
u8g.drawStr( 5, 45, "PROGRESS");
}
void u8g_Screen2() //Screen 2 - RECORDING STOPPED
{
u8g.setFont(u8g_font_9x18B);
u8g.drawStr( 5, 28, "RECORDING ");
u8g.drawStr( 5, 45, "STOPPED");
}
void draw(){
u8g.setFont(u8g_font_9x18B);
u8g.drawStr( 5, 28, "RECORDING IN");
u8g.drawStr( 5, 45, "PROGRESS");
}
void draw2(){
u8g.setFont(u8g_font_9x18B);
u8g.drawStr( 5, 28, "RECORDING ");
u8g.drawStr( 5, 45, "STOPPED");
}
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
pinMode(ledPin, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
//lcd
u8g.setFont(u8g_font_unifont);
u8g.setColorIndex(1); // Instructs the display to draw with a pixel on.
// flip screen, if required
//u8g.setRot180();
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(1); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
//u8g.setContrast(0x30);
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
ledState= digitalRead (ledPin);
//lcd
{
// check whether a pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on:
delay(5);
digitalWrite(ledPin, HIGH);
}
if (buttonState2 == HIGH) {
// turn LED on:
delay(5);
digitalWrite(ledPin, LOW); }
}
{
if (ledState == HIGH)
{
delay(2);
u8g_Screen1();
do
{
draw();
}
while (u8g_Screen2);
}
if (ledState == LOW)
{
delay(2);
u8g.firstPage();
do
{
draw2();
}
while (u8g.nextPage());
}
}
}
When I have the code only for the Led and not the LCD, the Led works as it's supposed to.
// constants won't change. They're used here to
// set pin numbers:
const int buttonPin1 = 9; // the number of the pushbutton pin 2
const int buttonPin2 = 10; // the number of the pushbutton pin 3
const int ledPin = 12; // the number of the LED pin
// variables will change:
int buttonState1 = 0; // variable for reading the pushbutton status
int buttonState2 = 0;
void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin1, INPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin2, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// read the state of the pushbutton value:
buttonState2 = digitalRead(buttonPin2);
// check whether a pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (buttonState1 == HIGH) {
// turn LED on: // Tweet that the washer is done
digitalWrite(ledPin, HIGH);
}
if (buttonState2 == HIGH) {
// turn LED on: //Tweet that the dryer is done
digitalWrite(ledPin, LOW);
}
}
The LCD works perfectly with this code: (it has the same display, but instead of buttons controlling the display, it plays the 2 screens on a loop)
/*
GraphicsTest.pde
>>> Before compiling: Please remove comment from the constructor of the
>>> connected graphics display (see below).
Universal 8bit Graphics Library, http://code.google.com/p/u8glib/
Copyright (c) 2012, olikraus@gmail.com
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "U8glib.h"
// A linha abaixo define as ligacoes e deve ser
// ajustada conforme o display utilizado.
U8GLIB_ST7920_128X64_1X u8g(6, 5, 4 ,7); //Enable, RW, RS, RESET
int display = 1;
void u8g_prepare()
{
u8g.setFont(u8g_font_6x10);
u8g.setFontRefHeightExtendedText();
u8g.setDefaultForegroundColor();
u8g.setFontPosTop();
}
void u8g_Screen1() //Screen 1 - RECORDING IN PROGRESS
{
u8g.setFont(u8g_font_9x18B);
u8g.drawStr( 5, 28, "RECORDING IN");
u8g.drawStr( 5, 45, "PROGRESS");
}
void u8g_Screen2() //Screen 2 - RECORDING STOPPED
{
u8g.setFont(u8g_font_9x18B);
u8g.drawStr( 5, 28, "RECORDING ");
u8g.drawStr( 5, 45, "STOPPED");
}
void draw() //Rotina Desenho
{
u8g_prepare();
switch(display) //Carrega a tela correspondente
{
case 1:
u8g_Screen1();
break;
case 2:
u8g_Screen2();
break;
}
}
void setup()
{
// flip screen, if required
//u8g.setRot180();
// assign default color value
if ( u8g.getMode() == U8G_MODE_R3G3B2 )
u8g.setColorIndex(255); // white
else if ( u8g.getMode() == U8G_MODE_GRAY2BIT )
u8g.setColorIndex(1); // max intensity
else if ( u8g.getMode() == U8G_MODE_BW )
u8g.setColorIndex(1); // pixel on
//u8g.setContrast(0x30);
}
void loop()
{
// picture loop
for (display =1; display < 3; display++) //Carrega as telas de 1 a 7
{
u8g.firstPage();
do
{
draw();
}
while( u8g.nextPage() );
delay(3000); //Pausa de 3 segundos e reinicia o processo
}
}
I also put the LCD onto a seperate power supply, because I thought the buttons eating up power might be causing disruption it getting the signal. But changed nothing either. (I attached a pic of the circuit.)
I'm very new to this whole thing and really trying, but no success.
Any pointers are welcomed!
Thank you!