I'm trying to create a device that will use and lcd display to present a message once a light dependent resistor detects light and control a servo from the push of a button.
My code concerning the servo works just fine but I can't seem to get my lcd display to work. Here are my error messages:
sketch_mar05a:71: error: 'lcd' does not name a type
lcd.begin(16,2);
^
sketch_mar05a:78: error: expected ';' at end of member declaration
void Update()
^
sketch_mar05a:80: error: 'ldrState' does not name a type
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
^
sketch_mar05a:82: error: expected unqualified-id before '{' token
{
^
sketch_mar05a:163: error: expected '}' at end of input
};
^
sketch_mar05a:73: error: expected unqualified-id at end of input
const int ldrPin = 7; // light dependent resistor pin
^
exit status 1
'lcd' does not name a type
Here's my code:
#include <Wire.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
class Twist
{
const int servoPin = 9; // servo pin
Servo servo; // the servo
int pos = 0; // variable to store the servo position
int directionState = 0;
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
const int buttonPin = 8; // pushbutton pin
int buttonState = 0;
public:
Twist(int interval)
{
updateInterval = interval;
increment = 1;
}
void Attach(int pin)
{
servo.attach(9);
}
void Detach()
{
servo.detach();
}
void Update()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
buttonState = digitalRead(buttonPin);
if (directionState == 0) {
if (buttonState == HIGH) {
directionState = 1;
for (pos = 20; pos < 180; pos = pos + 1) {
servo.write(pos);
}
}
} else if (directionState == 1) {
if (buttonState == HIGH) {
directionState = 0;
for (pos = 180; pos > 1; pos = pos - 1) {
servo.write(pos);
}
}
}
}
}
};
Twist twist1(15);
class Display
{
lcd.begin(16,2);
const int ldrPin = 7; // light dependent resistor pin
int ldrState; // local variable to hold ldr states
public:
void Update()
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
{
if (ldrState == HIGH)
{
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Open the damn");
lcd.setCursor(0,1); // Start at character 0 on line 1
lcd.print("blinds!");
{
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
}
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
}
}
}
};
Display display1();
void setup()
{
twist1.Attach(9);
}
void loop()
{
twist1.Update();
display1. Update();
};
void Update()
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
{
In any of your other functions, what goes between the ) and the {? Hint: NOTHING. Why did you try to change that here?
You really should define your classes in a header file and implement them in a source file. Then, you'd understand why the class knows nothing about the lcd instance.
Okay, so I'm not entirely sure if I did what you said to but I'm getting this error message now:
/Users/caitlinpeterson/Documents/Arduino/Blinds_Controller/Blinds_Controller.ino:13:19: fatal error: Twist.h: No such file or directory #include "Twist.h"
^
compilation terminated.
exit status 1
Error compiling for board Arduino/Genuino Uno.
Here's the modified code:
#ifndef Twist_h
#define Twist_h
#ifndef Display_h
#define Display_h
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
#include "Arduino.h"
#include "Twist.h"
#include "Display.h"
class Twist
{
const int servoPin = 9; // servo pin
Servo servo; // the servo
int pos = 0; // variable to store the servo position
int directionState = 0;
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
const int buttonPin = 8; // pushbutton pin
int buttonState = 0;
public:
Twist(int interval)
{
updateInterval = interval;
increment = 1;
}
void Attach(int pin)
{
servo.attach(9);
}
void Detach()
{
servo.detach();
}
void Update()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
buttonState = digitalRead(buttonPin);
if (directionState == 0) {
if (buttonState == HIGH) {
directionState = 1;
for (pos = 20; pos < 180; pos = pos + 1) {
servo.write(pos);
}
}
} else if (directionState == 1) {
if (buttonState == HIGH) {
directionState = 0;
for (pos = 180; pos > 1; pos = pos - 1) {
servo.write(pos);
}
}
}
}
}
};
Twist twist1(15);
#endif
class Display
{
lcd.begin();
const int ldrPin = 7; // light dependent resistor pin
int ldrState; // local variable to hold ldr states
public:
void Message1()
{
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
if (ldrState == HIGH)
{
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Open the damn");
lcd.setCursor(0,1); // Start at character 0 on line 1
lcd.print("blinds!");
{
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
}
void Message2()
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
}
}
}
}
};
Display diplay1();
#endif
void setup()
{
twist1.Attach(9);
}
void loop()
{
twist1.Update();
display1.Message1();
display1.Message2();
};
I'm not necessarily sure what you mean by including two guards in one file. In addition, I do have four different files. I followed this: https://www.arduino.cc/en/Hacking/LibraryTutorial -- in attempting to fix my code.
Here's my error message:
/Users/caitlinpeterson/Documents/Arduino/Blinds_Controller/Blinds_Controller.ino: In function 'void loop()':
Blinds_Controller:188: error: 'display1' was not declared in this scope
display1.Message1();
^
exit status 1
'display1' was not declared in this scope
Here's my current code:
#ifndef Twist_h
#define Twist_h
#include "Arduino.h"
#include "Twist.h"
#include <Servo.h>
class Twist
{
const int servoPin = 9; // servo pin
Servo servo; // the servo
int pos = 0; // variable to store the servo position
int directionState = 0;
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
const int buttonPin = 8; // pushbutton pin
int buttonState = 0;
public:
Twist(int interval);
void Attach(int pin);
void Detach();
void Update();
};
#include "Arduino.h"
#include "Twist.h"
Twist::Twist(int interval)
{
updateInterval = interval;
increment = 1;
}
void Twist::Attach(int pin)
{
servo.attach(9);
}
void Twist::Detach()
{
servo.detach();
}
void Twist::Update()
{
if((millis() - lastUpdate) > updateInterval) // time to update
{
lastUpdate = millis();
pos += increment;
buttonState = digitalRead(buttonPin);
if (directionState == 0) {
if (buttonState == HIGH) {
directionState = 1;
for (pos = 20; pos < 180; pos = pos + 1) {
servo.write(pos);
}
}
} else if (directionState == 1) {
if (buttonState == HIGH) {
directionState = 0;
for (pos = 180; pos > 1; pos = pos - 1) {
servo.write(pos);
}
}
}
}
};
#endif
Twist twist1(15);
#include "Display.h"
#include <Servo.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#ifndef Display_h
#define Display_h
class Display
{
lcd.begin();
const int ldrPin = 7; // light dependent resistor pin
int ldrState; // local variable to hold ldr states
public:
void Message1();
};
void Display::Message1()
{
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
if (ldrState == HIGH)
{
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Open the damn");
lcd.setCursor(0,1); // Start at character 0 on line 1
lcd.print("blinds!");
{
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
}
}
}
}
};
Display display1();
#endif
void setup()
{
twist1.Attach(9);
}
void loop()
{
twist1.Update();
display1.Message1();
};
#ifndef Twist_h
#define Twist_h
#include "Arduino.h"
#include "Twist.h"
#include <Servo.h>
class Twist
{
const int servoPin = 9; // servo pin
Servo servo; // the servo
int pos = 0; // variable to store the servo position
int directionState = 0;
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
const int buttonPin = 8; // pushbutton pin
int buttonState = 0;
public:
Twist(int interval);
void Attach(int pin);
void Detach();
void Update();
};
#endif
#ifndef Display_h
#define Display_h
#include "Display.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
class Display
{
const int ldrPin = 7; // light dependent resistor pin
int ldrState; // local variable to hold ldr states
public:
void Message1();
};
#endif
Here's Display.cpp:
void Display::Message1()
{
lcd.begin();
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
if (ldrState == HIGH)
{
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Open the damn");
lcd.setCursor(0,1); // Start at character 0 on line 1
lcd.print("blinds!");
{
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
}
}
}
}
New errors:
In file included from /Users/caitlinpeterson/Documents/Arduino/Blinds_Controller/Blinds_Controller.ino:1:0:
Display.h:12: error: 'lcd' does not name a type
lcd.begin();
^
/Users/caitlinpeterson/Documents/Arduino/Blinds_Controller/Blinds_Controller.ino: In function 'void loop()':
Blinds_Controller:20: error: request for member 'Message1' in 'display1', which is of non-class type 'Display()'
display1.Message1();
^
exit status 1
'lcd' does not name a type
#ifndef Twist_h
#define Twist_h
#include "Arduino.h"
#include <Servo.h>
class Twist
{
const int servoPin = 9; // servo pin
Servo servo; // the servo
int pos = 0; // variable to store the servo position
int directionState = 0;
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
const int buttonPin = 8; // pushbutton pin
int buttonState = 0;
public:
Twist(int interval);
void Attach(int pin);
void Detach();
void Update();
};
#endif
#ifndef Display_h
#define Display_h
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
class Display
{
lcd.begin();
const int ldrPin = 7; // light dependent resistor pin
int ldrState; // local variable to hold ldr states
public:
void Message1();
};
#endif
#include "Display.h"
void Display::Message1()
{
lcd.begin();
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
if (ldrState == HIGH)
{
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Open the damn");
lcd.setCursor(0,1); // Start at character 0 on line 1
lcd.print("blinds!");
{
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
}
}
}
}
I'm having a couple issues with my code. The code should be able to display a message on an lcd when a light dependent resistor senses light and operate a motor via a button push.
Here are my error messages:
In file included from /Users/caitlinpeterson/Documents/Arduino/Blinds_Controller/Blinds_Controller.ino:1:0:
Display.h:12: error: 'lcd' does not name a type
lcd.begin();
^
/Users/caitlinpeterson/Documents/Arduino/Blinds_Controller/Blinds_Controller.ino: In function 'void loop()':
Blinds_Controller:20: error: request for member 'Message1' in 'display1', which is of non-class type 'Display()'
display1.Message1();
^
exit status 1
'lcd' does not name a type
#ifndef Twist_h
#define Twist_h
#include "Arduino.h"
#include <Servo.h>
class Twist
{
const int servoPin = 9; // servo pin
Servo servo; // the servo
int pos = 0; // variable to store the servo position
int directionState = 0;
int increment; // increment to move for each interval
int updateInterval; // interval between updates
unsigned long lastUpdate; // last update of position
const int buttonPin = 8; // pushbutton pin
int buttonState = 0;
public:
Twist(int interval);
void Attach(int pin);
void Detach();
void Update();
};
#endif
#include "Arduino.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 16, 2);
class Display
{
lcd.begin(16, 2);
const int ldrPin = 7; // light dependent resistor pin
int ldrState; // local variable to hold ldr states
public:
void Message1();
};
#endif
Here's Display.cpp:
#include "Display.h"
#include <LiquidCrystal_I2C.h>
#include "Arduino.h"
void Display::Message1()
{
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
if (ldrState == HIGH)
{
lcd.clear();
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("Open the damn");
lcd.setCursor(0,1); // Start at character 0 on line 1
lcd.print("blinds!");
{
// scroll 13 positions (string length) to the left
// to move it offscreen left:
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
// scroll 29 positions (string length + display length) to the right
// to move it offscreen right:
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
// scroll one position right:
lcd.scrollDisplayRight();
}
// scroll 16 positions (display length + string length) to the left
// to move it back to center:
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
// scroll one position left:
lcd.scrollDisplayLeft();
}
{
lcd.clear();
for(int i = 0; i< 3; i++)
{
lcd.backlight();
lcd.noBacklight();
}
lcd.backlight();
lcd.setCursor(0,0);
lcd.print("Or else, innocent");
lcd.setCursor(0,1);
lcd.print("plants will die.");
{
{
for (int positionCounter = 0; positionCounter < 13; positionCounter++) {
lcd.scrollDisplayLeft();
}
for (int positionCounter = 0; positionCounter < 29; positionCounter++) {
lcd.scrollDisplayRight();
}
{
for (int positionCounter = 0; positionCounter < 16; positionCounter++) {
lcd.scrollDisplayLeft();
}
}
}
}
}
}
}
}
How long will it take this loop to tell the servo to go to position 179? What will the servo position be when the code tells the servo to go to position 179?
LiquidCrystal_I2C lcd(0x3F, 16, 2);
class Display
{
Why is the lcd instance not a member of the class?
ldrState = digitalRead(ldrPin); //read the digital state of ldrPin with digitalRead() function and store the value in ldrState variable
Does that comment provide ANY value? Anyone can see that digitalRead() is used, and that the result is stored in ldrState.
What needs explaining is why an analog device is being read as though it was a digital device.