# Convert C++ to Arduino Programming

Hi everyone. I am new to Arduino. I have two problem. First, I have a program in c++ language and I have some problem to convert it to Arduino programming (in C++ we use “cin” to ask user to enter answer but how about in Arduino?). Should anybody give me some guide and suggestion…I am using Arduino Uno and keypad 16 button.

second, Any idea to make a complete circuit that connect Keypad 16 button and Arduino. I already google about it but still have no solution.

Here some of C++ program and Arduino that I already try…
Thanks a lot for any help.

``````int main()
{
int sum;
int correct = 0;
int wrong = 0;
int i;
int j;

srand(time(0));

do {
cout << setw( 50 ) << "Kids Math Quiz Game!!!" << endl;
cout << setw( 49 ) << "**********Menu**********" << endl;
cout << setw( 44 ) << "[1] Addition " << endl;
cout << setw( 47 ) << "[2] Subtraction " << endl;
cout << setw( 40 ) << "[3] Exit " << endl << endl;

{
case 1:
i = rand()%10;
j = rand()%10;

if ((i<=1 && j<=9) || (i<=9 && j<=1))
{
cout << "What is " << i << " + " << j << " = ";
sum = i + j;

{
cout <<"Great Job!!!!!" << endl << endl;
correct++;
}
else
{
cout << "Sorry " << i <<" + " << j << " = " << sum <<endl << endl;
wrong++;
}

cout << "You got " << correct <<" right and " << wrong << " wrong"<< endl<< endl;
cin.get();
}
``````
``````#include <Keypad.h>

int sum;
int correct = 0;
int wrong = 0;
//int i;
//int j;
//long randNumber;
long i;
long j;

const byte ROWS = 4;  // four rows
const byte COLS = 4;  // four columns
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'#','0','*','D'}
};
byte rowPins[ROWS] = {8,1,2,4};  // Connect to the row pinouts of the keypad
byte colPins[COLS] = {3,5,6,7};  // Connect to the column pinouts of the keypad

byte ledPin = 13;

void setup()
{
Serial.begin(9600);    // Set up Serial library at 9600 bps
pinMode(ledPin, OUTPUT);      // Sets the digital pin as output
digitalWrite(ledPin, HIGH);   // Sets the LED on
}

void loop()
{

if (key != NO_KEY)
{
Serial.println(key);
}

{
delay(100);
}
}

{
//Serial.println(randomNumber);

Serial.println("  **KIDS MATH");
Serial.println(" QUIZ GAME!**");
Serial.println("[2] Subtraction");
Serial.println("[3] Exit");

switch (key)
{
case '1':
i = random(10);
j = random(10);

if ((i<=1 && j<=9) || (i<=9 && j<=1))
{
Serial.println("What is ");
Serial.print(i);
Serial.print(" + ");
Serial.print(j);
Serial.print(" = ");
Serial.println(i + j);
}
``````

Thanks

well, where do we start? An Arduino has no console unless you provide one. What kind of input/output or human-computer interface you need depends upon what you want your device to do. You can hook your Arduino up yo your PC using the USB cable, then use a terminal emulator program to talk to it. You then can use some code to read from and write to that serial port.

In Arduino, the setup() function is called when the device powers up and then the loop() function is called. I don’t know if the C++ stream IO library is implemented or if there is anything similar. You could do that I guess.

``````#include <NewSoftSerial.h>

// The arguments are the RX and TX pins, respectivrly
//NewSoftSerial mySerial(2, 3);

// Pins 0 and 1 are the system port that uses the USB header (RX, TX)
NewSoftSerial mySerial(0, 1);

void setup()
{
// set the data rate for the NewSoftSerial port
mySerial.begin(19200);
mySerial.println("Hello, world?");
}

void loop()                     // run over and over again
{
// Echo received keystrokes back out

if (mySerial.available()) {
}
}
``````
``````NewSoftSerial mySerial(0, 1);
``````

Unless you have some really good reason for using NewSoftSerial to read the hardware serial port data, don't!

There's a hardware serial class instance, Serial, for a reason.

instead of using keypad, we still need to have mySoftSerial? okay..I will try to include it. But, is it same mySerial.available and keypad.getkey()?Sorry for ask this question. I really don't know.

`````` if (mySerial.available()) {
``````
``````char key = keypad.getKey();

if (key != NO_KEY)
{
Serial.println(key);
}
``````

Well, I guess it depends upon how your keypad is hooked up to the Arduino.

On a PC, you have an operting system. The operating system has drivers that talk to the hardware, and provides an easy way for software to talk to it. In C++, that streams IO library abstracts it even further.

With an Arduino, you have to do it all yourself, or someone has to. There are libraries available that people have written, but my point is that you're working at a lever much closer to the hardware. So, you have to know how things are hooked up, then you have to write (or at least use) code that directly manipulates that hardware. If you want to turn a LED on, you need to write code that puts power to the pin on the chip tht the LED is hooked up to. If you want to see if someone pushed a button, you need to check the hardware to see if its state changed (ideally via an interupt service routine - ISR).