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. 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. I tried using the int main() and then placing the setup() function in that, and then using a while(1) { //place all of the loop code here} but it does not work at all. Below is my code, can anyone point me in the right direction. 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.
Below 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. 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. Thanks for anyones help.
I know this, that is why I am obviously asking for help. I have the arduino.h in there to test it. It works with the arduino.h which utilizes the void setup() and void loop(), but I need to get it to work with int main(). That is why I explained that it works when I comment out the serial functions, so the only arduino.h problem is I am utilizing the void loop() function, which we are not allowed to do.
You're just going to have to learn how to write C/C++ programs. I suspect that is the purpose of the exercise. When you eliminate Arduino.h, setup() and loop(), you are now programming, as we say, 'bare metal'. 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.
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. If you intend to establish anything that resembles a career based on embedded programming, this is fundamental knowledge you need.
Start by looking at Arduino.h and wore.h and see what functionality you're being asked to reproduce on your own. 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. If you don't understand main() then you don't understand basic C/C++ and perhaps that's a good place to start. There are numerous excellent tutorials available free online.
If however, you just want someone to do it for you, there are several eager-beavers in the forum ready to demonstrate their expertise with the language, or at least try to convince the rest of us that they've read a book or two, and will gladly re-write the code for you. I will not. If you want to learn, I and other will help.
I appreciate all of the insight. I have looked into the 2560 datasheet kind of heavily, and have utilized it for the entire semester thus far. This is the first time we have used communication and have not really learned anything about it. Page 246 of the Mega 2560 datasheet kind of lays out how to use I2C communication with conventional C code, but it is the information that is getting stored in TWDR that is kind of throwing me for a loop, and how to check what is in there. When looking at arduino.h all I see in there is them actually creating the functions for loop and setup, and I understand that setup is just being run one time, which is why I created a function for it and ran it once in main, and use the while loop to continuously run the code that was in the void loop. We have always used int main() and it appears to me that is how it should be gone about.
Thankfully I am an EE and this is my last coding based class at all. We are required to take two C/C++ classes and this micro controller class, but other than that there is no coding required for us. I have no desire whatsoever to do embedded programming or any programming for that manner besides simple matlab coding. I have done a couple internships in design, circuit analysis, etc. and plan to stick with that route.
In the twi.h and the wire.h I see the registers they are calling and what bits they are assigning in some of the registers for the Arduino's, so it it somewhat clear. The main confusion is how to just get the data from the VL53L0X. I understand we should be initializing the SCL and SDA lines, initializing the I2C by setting the prescalers correctly and getting the clock frequency by utilizing the I2C clock equation, then starting I2C using the start condition, wait for the end of a transmission, checking for the flag, loading the address (I believe 0x52 for write and 0x53 for read for this sensor), and waiting until the flag goes down to check for the end of the transmission. Then we must write the data, have a read acknowledgment, and then continue sending one until there is an nack, and then stop the transmission. Somewhere in there some data should be stored in TWDR, but I am not entirely sure how to get it, or if it is done correctly. I can't press forward without getting the int main function to work correctly before I start working on the I2C portion though.
Unfortunately, TWI is perhaps THE most confusing bit of hardware on the chip. No easy way around it. Otherwise, in the same directory that you found Arduino.h you'll find main.cpp. Have a look at that and the mystery between main/setup/loop will be resolved. Setup and loop are merely functions being called within main. The contents of those functions are what's important. If coding in C/C++ the compiler looks for the main() function as the insertion point for program execution. In the Arduino IDE this is hidden from you in main.cpp, but instead calls setup() and then loops through loop(). By writing your code such that you are using main() instead of setup/loop, this is a signal to the Arduino builder to NOT include all the Arduino stuff, so you have to do it all yourself. Set up timer0 for millis() and micros(), configure the ports for PWM, and all the rest. Your instructor obviously wants you exposed to the platform. Push comes to shove, you can always copy the code from the libraries into your own program.
As a new soon-to-be-EE I find it difficult to accept your vision of a future being devoid of programming, unless you plan to hire out the work. Just regard it as another tool on your workbench, one that in the future you'll be glad you have.
This is actually the first program I have ever built using the Arduino library. The other projects have all worked only using the int main(), but anything that I needed to loop through, I always just stuck in a while(1) loop, which is why I was confused when it didn't work on this one. No worries though, I will figure it out.
pert:
Start by getting a simple blink program running, using your own main() definition and without using the Arduino core library.
That's a big start as you'll have to replicate the millis() functionality or look to the avr-libc library for the compiler-supplied equivalent. Check here.
Thanks guys, I have at least gotten some signal being sent using my own code and no arduinos. Just have to mess with figuring out why all my numbers are random now.
It just returns a 0 for normal exit usually, that is how we always used it in practice anyway. We always used int main for everything. I have never coded with void main honestly.
PowerPro25:
It just returns a 0 for normal exit usually, that is how we always used it in practice anyway. We always used int main for everything. I have never coded with void main honestly.
OK, but what calls it and where does it return to ?