Simple Arduino Input

When I can find time, I’ve been working on my cat feeder code (or researching DIY CNC routers).  For now I am keeping the electronic components simple, so the time and alarms will be set through the USB serial connection.

I use Visual Micro, a free Arduino programming plugin for Visual Studio 2010-2013 (I use VS Community 2013, which is free for Windows), to program my Arduino micro controllers.

To read the text that a user inputs from the serial monitor, I created a variable to save the data to as an array of characters.

[code lang=”cpp”]
char inData[3];
[/code]

During the loop function, I check to see if an input had been sent to the Arduino (using Serial.available), and if so, read the information into my inData variable. Then, using my array of characters stored in inData, I convert the numbers from char* to an int variable (inNumber) using atoi().

[code lang=”cpp” firstline=”2″]
if (Serial.available() >= 0) {
Serial.readBytes(inData, 3);
inNumber = atoi(inData);
[/code]

If you look closely, I didn’t close that IF statement right away. I put all of my code that decides what to do with that newly input number. Using a switch that reads the value of a variable (an enum) I use to keep track of what kind of an input the program is waiting for.

[code lang=”cpp” firstline=”5″]
switch (currentInput) {
case MAIN_MENU: MainMenuSelection(inNumber); break;
case SET_HOUR: SetCurrentHour(inNumber); break;
case SET_MINUTE: SetCurrentMinute(inNumber);
}
} // End of IF statement
[/code]

To simplify looking through my code, I let the switch statement call other functions, passing my input number to them.

That’s all I’m sharing for now! Hopefully I helped you find a way to send messages to your Arduino.

Cheers!

Leave a Message!

Your email address will not be published. Required fields are marked *