I want to learn the terms used in C++ language and the equivalents of their functions in Arduino. How can I write a code written in C++ on Arduino? For example
"#include #include #include
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int number, first, last, result;
list multipliers;
cout << "Enter number: ";
cin >> number;
float coke = sqrt(number);
if (kok != round(kok)) {
cout << "The square root of the number entered does not exist";
return 0;
}
for(int a = 1; a<number; a++){
if (number%a == 0) {
multipliers.push_back(a);
if (a*a == number)
break;
}
}
first = multipliers.front();
end = multipliers.back();
result = (first + number) + (last +(number/last));
cout << "Next number of integers: " << result;
return 0;
}
How can I convert this code to Arduino?
Kind regards,
The Arduino is programmed in C++ but there are things that it cannot do. For instance, where would the cout output go to ?
You do not have to use the setup() amd loop() program structure. If you create your own main() function then it will replace the hidden main() function that the Arduino environment uses behind the scenes
Here is the Arduino main() function refereed to above
/*
main.cpp - Main loop for Arduino sketches
Copyright (c) 2005-2013 Arduino Team. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
// Declared weak in Arduino.h to allow user redefinitions.
int atexit(void (* /*func*/ )()) { return 0; }
// Weak empty variant initialization function.
// May be redefined by variant files.
void initVariant() __attribute__((weak));
void initVariant() { }
void setupUSB() __attribute__((weak));
void setupUSB() { }
int main(void)
{
init();
initVariant();
#if defined(USBCON)
USBDevice.attach();
#endif
setup();
for (;;) {
loop();
if (serialEventRun) serialEventRun();
}
return 0;
}
If you have a computer to program the arduino, why not learn C++ directly there (compilers are free those days) rather than going through the limited arduino board?