but when i display the button value to LCD, it's not increment by one . It's displaying some random value it think and the conditions are not satisfying. like reseting to 0 when it reaches 24 and all
please look into my below code and give me right instructions
#include <LiquidCrystal.h>
#define MOTOR1 3
#define SWITCH1 11
#define SWITCH2 12
int RunNo = 0;
int clock_hour = 0;
int clock_mins = 0;
int second=0, minute=0, hour=0;
int x=0;
LiquidCrystal lcd(9, 8, 5, 4, 6, 7);
void setup(){
pinMode(MOTOR1,OUTPUT);
pinMode(SWITCH1,INPUT_PULLUP);
pinMode(SWITCH2,INPUT_PULLUP);
Serial.begin(115200);
lcd.print("load...");
delay(1000);
lcd.begin(16, 2);
}
void loop(){
int switch_hour = digitalRead(SWITCH1);
int switch_mins = digitalRead(SWITCH2);
if (switch_hour == LOW){
clock_hour = clock_hour + 1;
if (clock_hour >= 24){
clock_hour = 0;
}
}
if (switch_mins == LOW){
clock_mins = clock_mins + 1;
if (clock_mins >= 60){
clock_mins = 0;
}
}
print_lcd(clock_hour,clock_mins);
static unsigned long lastTick = 0;
if (millis() - lastTick >= 1000) {
lastTick = millis();
second++;
}
if (second >= 60) {
minute++;
second = 0;
}
if (minute >=60) {
hour++;
minute = 0;
}
if (hour >=24) {
hour=0;
minute = 0;
}
if (clock_hour == 0 && clock_mins == 0 && hour == 0 && minute == 0){
int val = 1;
}
else if(clock_hour == hour && clock_mins == minute )
{
feederOn();
RunNo = RunNo + 1;
delay(2);
feederOff();
}
digitalClockDisplay();
}
void printDigits(byte digits){
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
char sep()
{
x = millis()/500;
if(x%2==0)
{
lcd.print(":");
}
else{
lcd.print(" ");
}
}
void digitalClockDisplay(){
lcd.setCursor(0,0);
printDigits(hour);
sep();
printDigits(minute);
sep();
printDigits(second);
lcd.print(" Cont:");
lcd.print(RunNo);
}
int feederOn(){
digitalWrite(MOTOR1,HIGH);
delay(350);
}
int feederOff(){
digitalWrite(MOTOR1,LOW);
}
int print_lcd(int i,int j)
{
lcd.setCursor(0, 1);
lcd.print("Set To: ");
lcd.print(i);
lcd.print(":");
lcd.print(j);
}
I am struggling a little with the new Forum - I am trying to extract part of your code to make a short sketch.
I think what I have here will work and should explore your problem without confusing ourselves with the rest of the code. I don't have an LCD to try the code.
The only change I have made from your code is to add a short delay() to slow things down a bit. It is possible that the delay is essential - though it would probably be better in your real code to use the BWoD technique rather than the delay() function.
Try this short version and see if you can get it to work.
#include <LiquidCrystal.h>
#define MOTOR1 3
#define SWITCH1 11
#define SWITCH2 12
int RunNo = 0;
int clock_hour = 0;
int clock_mins = 0;
int second=0, minute=0, hour=0;
int x=0;
LiquidCrystal lcd(9, 8, 5, 4, 6, 7);
void setup(){
pinMode(MOTOR1,OUTPUT);
pinMode(SWITCH1,INPUT_PULLUP);
pinMode(SWITCH2,INPUT_PULLUP);
Serial.begin(115200);
lcd.print("load...");
delay(1000);
lcd.begin(16, 2);
}
void loop(){
int switch_hour = digitalRead(SWITCH1);
int switch_mins = digitalRead(SWITCH2);
if (switch_hour == LOW){
clock_hour = clock_hour + 1;
if (clock_hour >= 24){
clock_hour = 0;
}
}
if (switch_mins == LOW){
clock_mins = clock_mins + 1;
if (clock_mins >= 60){
clock_mins = 0;
}
}
print_lcd(clock_hour,clock_mins);
delay(200);
}
yes i want to change the numbers while press-release. suppose current number is displayed on LCD is 2, when i press the button, it should add +1 to it ans display 3.
Seems like delay is working . i added delay(100); and the fast number scrolling is gone.
again another issue came
void loop(){
int switch_hour = digitalRead(SWITCH1);
int switch_mins = digitalRead(SWITCH2);
if (switch_hour == LOW){
clock_hour = clock_hour + 1;
if (clock_hour >= 24){
clock_hour = 0;
}
}
if (switch_mins == LOW){
clock_mins = clock_mins + 1;
if (clock_mins >= 60){
clock_mins = 0;
}
}
print_lcd(clock_hour,clock_mins);
the "clock_hour" is increasing and it showing upto 24, after 24, it's resetting to 0. when the "clock_hour" value is resetting to 0, the "clock_mins" is showing some random number that also above 60.
HI
i have created a sample front panel with button arrangements. all buttons are not listed in code. but i need to do the same things from the frontpanel.
if anyone had done a project like this. please give me some guidelines and suggestions. as i am very new to this
What have you tried?
What do you think might be possible causes?
At the moment I have the impression that you are treating the project like building with Lego bricks - stick them together and they stand up. Programming needs a lot of careful thought. That's why you need to start with small pieces and get them working - it is easier to understand where a problem lies in 10 lines of code rather than 100. Have you written a small sketch (just using the Serial Monitor) to learn how to use press button switches so they do what you want? Do you understand why the delay makes a difference?
When you are dealing with contact meets contact switches/buttons there will be a short time when the contact is on and off, sparking in the very small space. This time may last less than 1/1000 seconds or as long as 20/1000 seconds of more depending on the physical switch and the press.
The time is too short for a human to detect but Arduino runs at 16 MHz and can easily detect. Code that does not account for this can be fooled and read intentions falsely.
The different ways to handle that are called debouncing. One of the simplest ways is to simply wait after the pin goes high and not read all the bounces. Another way is to read the bounces and watch for the pin state to be stable for some small time. There is even debouncing by hardware, components that absorb the sparking but really why spend parts when code can count it away?
i don't know this project will work as i panned. with my code. please correct me if my idea and code is wrong. you guyz will better get nice idea how it will be.
If this is the only Arduino project you will ever do then I suggest you ask someone else to write the code for you.
But if you are interested in the Arduino as a hobby then you need to learn the basics. If you work through several of the examples that come with the Arduino IDE and some of the online tutorials you will encounter most of the concepts that you need for this project.
If you make a short sketch to experiment with push-buttons you can learn by experience what happens when buttons get pushed and why in some circumstances you need code to eliminate switch bounce.
Any time I come to something new I make short sketches to enable me to learn how to work with it.
if (millis() - lastTick >= 1000) {
lastTick = millis(); second++; <- change to another variable ie Counter (type cast as unsigned long)
}
if (second >= 60) {
minute++;
second = 0;
}
if (minute >=60) {
hour++;
minute = 0;
}
if (hour >=24) {
hour=0;
minute = 0;
}
and use the module operator(%) for the seconds, minutes and hours. No need to check if they go over 60 or 24, the module operator will handle that for you.
When I have a new thing I am not sure of, first I spend time experimenting with it. Like a sensor or button, I try to find out how it will behave when I do things with it. I write simple sketches just to learn about the new thing. I do not try to wrap it up in an end project and learn that way.
You know some things about C code and little about the button. Spend time learning about just the button and then each part you are not sure of, make small easy things to learn those parts. When you know the parts then see about how best to fit them together.
i have almost completed my project . all the button issues been fixed. i did a re-code of this
currently i am facing some issues with time. yesterday night i set same time as my laptop(23:15) and when i check in the morning, there is around 20 minutes difference. idon't know how it happend.
please check below code and correct me if some blunder is there:
#include <LiquidCrystal.h>
#define MOTOR1 10
#define SWITCH1 11
#define SWITCH2 12
#define SWITCH3 3
#define SWITCH4 2
int clock_hour = 6;
int clock_minute = 0;
int check_sec = 1;
int motor_on = 0;
int second=0, minute=0, hour=0;
int x=0;
LiquidCrystal lcd(9, 8, 5, 4, 6, 7);
void setup(){
pinMode(MOTOR1,OUTPUT);
pinMode(SWITCH1,INPUT_PULLUP);
pinMode(SWITCH2,INPUT_PULLUP);
pinMode(SWITCH3,INPUT_PULLUP);
pinMode(SWITCH4,INPUT_PULLUP);
Serial.begin(115200);
lcd.print("load...");
delay(1000);
lcd.begin(16, 2);
}
void loop(){
int clock_set_hour = digitalRead(SWITCH2);
int clock_set_minute = digitalRead(SWITCH1);
int timer_set_hour = digitalRead(SWITCH3);
int timer_set_minute = digitalRead(SWITCH4);
delay(250);
if (clock_set_hour == LOW){
hour++;
}
if (clock_set_minute == LOW){
minute++;
}
if (timer_set_hour == LOW){
clock_hour++;
if (clock_hour >= 24){
clock_hour = 0;
}
}
if (timer_set_minute == LOW){
clock_minute++;
if (clock_minute >= 60){
clock_minute = 0;
}
}
Print_Lcd(clock_hour,clock_minute);
static unsigned long lastTick = 0;
if (millis() - lastTick >= 1000) {
lastTick = millis();
second++;
}
if (second >= 60) {
minute += second / 60;
second = second % 60;
}
if (minute >= 60) {
hour += minute / 60;
minute = minute % 60;
}
if (hour >= 24) {
hour = hour % 24;
}
if (clock_hour == hour && clock_minute == minute && check_sec == second){
// if (clock_hour == hour && clock_minute == minute){
feederOn();
delay(2);
feederOff();
}
digitalClockDisplay();
}
void printDigits(byte digits){
if(digits < 10)
lcd.print('0');
lcd.print(digits);
}
char sep()
{
x = millis()/500;
if(x%2==0)
{
lcd.print(":");
}
else{
lcd.print(" ");
}
}
void digitalClockDisplay(){
lcd.setCursor(4,0);
printDigits(hour);
sep();
printDigits(minute);
sep();
printDigits(second);
}
void Print_Lcd(int i,int j){
lcd.setCursor(2,1);
lcd.print("Set To-> ");
lcd.print(i);
lcd.print(":");
lcd.print(j);
}
void feederOn(){
delay(700);
digitalWrite(MOTOR1,HIGH);
delay(350);
}
void feederOff(){
digitalWrite(MOTOR1,LOW);
}
bentech4u:
currently i am facing some issues with time. yesterday night i set same time as my laptop(23:15) and when i check in the morning, there is around 20 minutes difference. idon't know how it happend.
The Arduino does not run at precisely 16MHz. If you want to keep to exact clock time you need to use a Real Time Clock RTC module.
The UNO does not use a crystal but instead a less precise resonator for oscillator.
You can build an Arduino on a breadboard that will hold better time just by using a crystal.
Also your code delays 1/4 second every time around.
void loop(){
int clock_set_hour = digitalRead(SWITCH2);
int clock_set_minute = digitalRead(SWITCH1);
int timer_set_hour = digitalRead(SWITCH3);
int timer_set_minute = digitalRead(SWITCH4);
delay(250); // right here
Also the compiler sees 1000 and makes an int...
if (millis() - lastTick >= 1000) { // should be 1000UL
Home AC is pretty rock-solid whether 50 or 60 cycle. You can use just 1 wire (not paired, electric cords are pairs of wire) to induce AC current into a coil. Put the coil through a diode and read the pulse on the + pulse with an analog pin.
I don't know how many turns of coil it will take to get a readable signal but not too many I'm sure.
So count pulses instead of millis. Pulses are slower.
If you want to research, look up physics induction coil. The power company will be your clock driver. This will only take wire to sense with. How is that for no components?