Hi, I'm new to the world of Arduino and I'm right now starting to build my own shutter for an old wooden camera of mine. I haven't previously coded enough to say that I understand the language and therefore my coding often consists of copy/paste other peoples work. This of course is a problem when I want to modify parts that I don't understand. Therefore I need help.
As of right now my shutter consist of an arduino uno, a servo (the motor may change in the future when the shutter will be constructed but it doesn't matter right now) and a lcd shield with five buttons. The code that "I've" written works almost just as I want it to but it is the next step that gives me some problems.
The code as right now looks as following:
/*-----( Import needed libraries )-----*/
#include <LiquidCrystal.h>
#include <Servo.h>
/*-----( Declare objects )-----*/
LiquidCrystal lcd(8, 9, 4, 5, 6, 7); //These are the pins used on this shield
/*-----( Declare Constants )-----*/
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
/*-----( Declare Variables )-----*/
int lcd_key = 0;
int adc_key_in = 0;
int adc_key_prev = 0;
int shutter = 0;
Servo myservo;
void setup() /*----( SETUP: RUNS ONCE )----*/
{
myservo.attach(0);
lcd.begin(16, 2); // set up the LCD's number of columns and rows
lcd.clear(); // Clear LCD
myservo.write(50);
delay(1000);
myservo.write(180);
lcd.setCursor(0,0);
lcd.print("Shutter:");
}/*--(end setup )---*/
void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
{
adc_key_prev = lcd_key ; // Looking for changes
lcd_key = read_LCD_buttons(); // read the buttons
lcd.setCursor(0,1); // move to the begining of the second line
switch (lcd_key) // depending on which button was pushed, we perform an action
{
case btnRIGHT:
{
lcd.setCursor(9,0);
lcd.print ("Opened ");
myservo.write(50);
break;
}
case btnLEFT:
{
lcd.setCursor(9,0);
lcd.print ("Closed ");
myservo.write(180);
break;
}
case btnUP:
{
if (shutter < 32000)
{
shutter += 1000;
}
lcd.setCursor(10,0);
lcd.print (" ");
lcd.setCursor(9,0);
lcd.print (shutter / 1000);
lcd.setCursor(11,0);
lcd.print ("sec");
break;
}
case btnDOWN:
{
if (shutter > 0)
{
shutter -= 1000;
}
lcd.setCursor(10,0);
lcd.print (" ");
lcd.setCursor(9,0);
lcd.print (shutter / 1000);
lcd.setCursor(11,0);
lcd.print ("sec");
break;
}
case btnSELECT:
{
lcd.setCursor(10,0);
lcd.print (" ");
lcd.setCursor(9,0);
lcd.print (shutter / 1000);
lcd.setCursor(11,0);
lcd.print ("sec");
lcd.setCursor(0,1);
lcd.print ("Run shutter");
myservo.write(50);
delay(shutter);
myservo.write(180);
break;
}
case btnNONE:
{
lcd.print(" ");
break;
}
}/* --(end switch )-- */
}/* --(end main loop )-- */
/*-----( Declare User-written Functions )-----*/
int read_LCD_buttons()
{
adc_key_in = analogRead(0); // read the value from the sensor
delay(5); //switch debounce delay. Increase this delay if incorrect switch selections are returned.
int k = (analogRead(0) - adc_key_in); //gives the button a slight range to allow for a little contact resistance noise
if (5 < abs(k)) return btnNONE; // double checks the keypress. If the two readings are not equal +/-k value after debounce delay, it tries again.
// my buttons when read are centered at these valies: 0, 144, 329, 504, 741
// we add approx 50 to those values and check to see if we are close
if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
if (adc_key_in < 50) return btnRIGHT;
if (adc_key_in < 195) return btnUP;
if (adc_key_in < 380) return btnDOWN;
if (adc_key_in < 555) return btnLEFT;
if (adc_key_in < 790) return btnSELECT;
return btnNONE; // when all others fail, return this...
}
Now I've come up with this brilliant idea that I want to include an IR remote and receiver, so I bought one. I got the remote to work with the servo alone, but here is the problem, I don't know how to combine the two scripts.
The script I used for the IR-part:
#include <IRremote.h>
int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin
int RECV_PIN = 11; //Connect the receiver to pin 11
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
Serial.begin(9600);
irrecv.enableIRIn(); // Starting the receiver
}
void loop() {
if (irrecv.decode(&results)) {
switch (results.value) {
case 0xFF6897:
Serial.println("1");
break;
case 0xFF9867:
Serial.println("2");
break;
case 0xFFB04F:
Serial.println("3");
break;
case 0xFF30CF:
Serial.println("4");
break;
case 0xFF18E7:
Serial.println("5");
break;
case 0xFF7A85:
Serial.println("6");
break;
case 0xFF10EF:
Serial.println("7");
break;
case 0xFF38C7:
Serial.println("8");
break;
case 0xFF5AA5:
Serial.println("9");
break;
case 0xFF4AB5:
Serial.println("0");
break;
case 0xFF42BD:
Serial.println("*");
break;
case 0xFF52AD:
Serial.println("#");
break;
case 0xFF02FD:
Serial.println("OK");
break;
case 0xFF22DD:
Serial.println("LEFT");
break;
case 0xFF629D:
Serial.println("UP");
break;
case 0xFFC23D:
Serial.println("RIGHT");
break;
case 0xFFA857:
Serial.println("DOWN");
break;
}
irrecv.resume(); // Receive the next value
}
}
The best way, in my head to solve this, is if I could combine the buttons on the lcd with the button on the remote. If case 0xFFC23D along with btnRIGHT could perform the task that is needed, but from what I've read is that you can't have multiple cases, so how do I reconstruct my code to make this possible? Or do I write separate cases for each separate button?
Kind regards
Ludvig Holmkvist