![]() There are numerous excellent tutorials available free online. If you don't understand main() then you don't understand basic C/C++ and perhaps that's a good place to start. It's not a lot and understanding how to do it yourself will gain you much more than asking someone to lay it out for you. Start by looking at Arduino.h and wore.h and see what functionality you're being asked to reproduce on your own. If you intend to establish anything that resembles a career based on embedded programming, this is fundamental knowledge you need. If you have been instructed that you can't use the fundamental libraries that the Arduino IDE employs to hide the hardware from you, it's likely because you are being asked to think about the underlying platform as something more than an abstraction. At this point a good reference manual for C/C++, a copy of the avr-libc manual and the datasheet for the 2560 are your best friends. When you eliminate Arduino.h, setup() and loop(), you are now programming, as we say, 'bare metal'. I suspect that is the purpose of the exercise. You're just going to have to learn how to write C/C++ programs. I can use Arduino.h and comment out the serial.begin(9600) and the print statement and it will still work fine, I just can't figure out how to use int main instead of the loop. We are also not aloud to use the wire library and we have to use the specific TWI registers for the Mega 2560 but that is another problem I will figure out later.īelow is my code: the setup() function is at the bottom of the VL53L0X.cpp file and the initLED() is just initializing the LED's by assigning the appropriate pins as outputs. Below is my code, can anyone point me in the right direction. I tried using the int main() and then placing the setup() function in that, and then using a while(1) but it does not work at all. I am using the VL53L0X sensor to set up two leds, one will be on when the reading is >400mm and then the other will be on when it is less than 400mm. You should use local variables for things that are really local, but the nature of embedded programming says that you are more likely to have variables that ARE and should be genuinely global as well.First of all, we are not allowed to use the arduino.h library for this project, and I have been messing around with it for hours. Or that have the loop-type structure that arduino sketches use. But most C++ sites are unlikely to take into account the details of getting a program working on a small microcontroller with only 2K of memory. ![]() I've read on C++ sites that it is generally frowned upon. A global variable MUST occupy a memory location, for example, while the compiler can optimize a local variable so that it never leaves a register. ![]() There is some overhead associated with creating the local variables, but there is also potentially LESS overhead in accessing them inside the function. They are recreated each time the function runs, and are destroyed when the function exits (unless you also declare them "static.") So there is no danger of running out of space after you "redefine" them thousands of times. Part of the advantage to local variables is that there are NOT accessible to other functions this means you can use a popular variable name like "temp" or "i" without worrying that you'll overwrite some other value of temp or i. The variable itself will (or can) exist in storage that disappears when setup() exits. When you declare a variable inside of a function like setup(), that variable NAME is only available inside THAT function. Is there a downside to this approach (for example, are all of the variables redefined each time the loop comes around)?īecause the "scope" of a variable which is where is can be reference from is defined by where the variable is declared. If I don't want ANY global variables, would I declare and initialize them all in loop() as follows?.if arduino calls setup() at the start of a program, why aren't variables defined there available to loop()?.Please look at the code below: int bodyvar=2 įirst note that this program does NOT compile as it gives the following errorĮrror: 'setupvar' was not declared in this scope In order for me to do so, I need to better understand how Arduino handles scope of variables in three areas: the body, the setup function and the loop function. I am trying to avoid using global variables as I've read on C++ sites that it is generally frowned upon.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |