I am tryiing to build a menu system to allow paramter changes for a "water drop" controller
The final product will ask me:
- for the number of drops ( 1, 2, or 3 drops)
- how long to open the valve in millisecs (control drop size)
- how much drop separation between drops (in millisecs) if there are multiple drops
The controller will then open and close a valve to generate the drop stream.
I am having trouble with the following code. I do not understand what
this statement does. int get_key(unsigned int input) and why it
does not seem to need the ; at the end of it.
The first example code compiles and runs on my Arduino Uno and the DFRobot LCD Kepad Shield.
When I try and incorporate this code as a subroutine I get a compiler error.
Here is the error message:
NEW_MENU_REV3.cpp: In function 'void drpcount()':
NEW_MENU_REV3:188: error: a function-definition is not allowed here before '{' token
OutLoop_Rev1:68: error: expected `}' at end of input
The curly brace following this statement: int get_key(unsigned int input) is highlighted
Here is the code that compiles without error:
#include <LiquidCrystal.h>
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
char msgs[5][15] = {
"Right Key OK ", // Value = 0
"Up Key OK ", // Value = 1
"Down Key OK ", // Value = 2
"Left Key OK ", // Value = 3
"Select Key OK" }; // Value = 4
int adc_key_val[5] ={
30, 150, 360, 535, 760 };
int NUM_KEYS = 5;
int adc_key_in;
int key=-1;
int oldkey=-1;
int loopcount = 0;
int DropCount = 2;
int pulseWidth0 = 1000; //Valve ON Time (Drop Size)
int pulseWidth1 = 1000; //Valve ON Time (Drop Size)
int pulseWidth2 = 1000; //Valve ON Time (Drop Size)
int pulseSep0 = 300; // Drop Seperation time (Time Between Drops)
int pulseSep1 = 350; // Drop Seperation time (Time Between Drops)
int pulseSep2 = 400; // Drop Seperation time (Time Between Drops)
int sensorValue =0;
int mapvalue = 0;
int reading = 0;
int backLight = 10;
int selectPin = 6;
int valveControl =11; // Output pin to turn valve ON & OFF
int ledpin = 13;
int ReadCount = 0;
void setup()
{
pinMode(13, OUTPUT); //we'll use the debug LED to output a heartbeat
pinMode(11,OUTPUT);
pinMode(10,OUTPUT);
lcd.begin(16,2);
//optionally, now set up our application-specific display settings, overriding whatever the lcd did in lcd.init()
digitalWrite(13, LOW); // Turn OFF board LED
digitalWrite(backLight,HIGH); // Turn ON LCD Backlight
lcd.clear();
lcd.print("Drop Count");
lcd.setCursor(13,0);
lcd.print(DropCount);
lcd.setCursor(13,0);
//lcd.cursor(); //Turn on the cursor
}
void loop()
{
adc_key_in = analogRead(0); // read the value from the sensor
//digitalWrite(13, HIGH);
key = get_key(adc_key_in); // convert into key press
if (key != oldkey) // if keypress is detected
{
delay(50); // wait for debounce time
adc_key_in = analogRead(0); // read the value from the sensor
key = get_key(adc_key_in); // convert into key press
if (key != oldkey)
{
oldkey = key;
if (key >=0)
{
lcd.setCursor(13, 0); //line=1, pos 13
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,0);
lcd.print("Drop Count");
lcd.setCursor(13,0);
lcd.print(DropCount);
lcd.setCursor(13,0);
}
if (key == 1)
{
DropCount = DropCount++;
if (DropCount == 4)
{
DropCount = 3;
lcd.setCursor(0,1);
lcd.print("Max Drops = 3");
lcd.setCursor(13,0);
}
lcd.print(DropCount);
lcd.setCursor(13,0);
}
else if (key == 2)
{
lcd.setCursor(13, 0); //line=1, pos 13
DropCount = DropCount--;
lcd.print(DropCount);
lcd.setCursor(13,0);
if (DropCount == 0)
{
DropCount = 1;
lcd.setCursor(0,1);
lcd.print("Min Drops = 1");
lcd.setCursor(13,0);
}
lcd.print(DropCount);
lcd.setCursor(13,0);
}
}
}
else if (key == 4)
{
// outloop();
delay(1000);
lcd.setCursor(13,0);
lcd.print(DropCount);
lcd.setCursor(0,1);
}
}
// Convert ADC value to key number
int get_key(unsigned int input)
{
int k;
for (k = 0; k < NUM_KEYS; k++)
{
if (input < adc_key_val[k])
{
return k;
}
}
if (k >= NUM_KEYS)
k = -1; // No valid key pressed
return k;
}
The code incorporating the first sketch as a subroutine will be posted in a second message because of
message size limitations:
I am at a loss as to why it compiles in the first example but generates errors in the second.
Did I miss a declaration or am I missing one of these ; somewhere?