[2017.6 Digital Twin] First Glance with Arduino

First Glance with Arduino

Though I have before helped my friends with their arduino project. But this is my first time doing myself project with arduino. So far I know that the arduino is based on Atmal Mega Chip. One of my friends had a project,which use this chip. It is a AVR(I am not sure) system using C. Arduino just build a framework on it. And it changes the troditional programming custom. Users, i.e. Programmer, do not need to consider about the C files structure, system basic running(like while(1)) etc. More important is that arduino gives users a lot of usefull libraries. But using arduino IDE means that you also give up the details of the libraries. You can't directly see it cause it is hiding in the arduino system. That means, when you want to see how a function that comes from the library work, or you want to build a private/personal function that works with other sensor or devices, it is little hard( I think, because you jump over the system layer)

Ok, no nonsense any more, let's start with Arduino

From website I can find some help

Some Example like this
File > Examples >01.Basics > Blink.

// the setup function runs once when you press reset or power the board

void setup() {

  // initialize digital pin LED_BUILTIN as an output.

  pinMode(LED_BUILTIN, OUTPUT);

}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_BUILTIN, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
}

als you can see, the ardunio hides the dital of the function in the basic layer, users can directly use them but cann't change them, like the function 
pinMode
digitalWrite
delay
so it is necessary for me to get a "dictionary" of these function, how to use them and the process details.

....

OK, I find something, like this
https://www.arduino.cc/en/Hacking/LibraryTutorial
here talk about how to make a self library. And in this article we can see the detail about how to work of the library. actually, it uses C++ to build a class. and in this class there are two major functions : setup() and Loop(). I think, for arduino system, it has a basic system, when you "upload" your code, actually you just write a class and upload it to the system. the system works like a thread (C++). and thread calls the different object's loop() function.

So as i said, when you want to build a self library or function of a new sensor, it just turn back to the beginning of AVR program. I have check one of the library example, and can see, it has no different with the program directly on the AT Mega.


But. for my project. it is not necessary to study how to make a self library. just use ready-made. Easy and no problem.


Some of the usefull functions are here
Here I write down some important functions

pinMode()

pinMode(pin, mode)


pin: the number of the pin whose mode you wish to set (int)
mode: INPUT, OUTPUT, or INPUT_PULLUP.
Input
Pins configured in a high-impedance state. Input pins make extremely small demands on the circuit that they are sampling, equivalent to a series resistor of 100 megohm in front of the pin.

Input_Pullup
The value of this pullup depends on the microcontroller used. On most AVR-based boards, the value is guaranteed to be between 20kΩ and 50kΩ.
When connecting a sensor to a pin configured with INPUT_PULLUP, the other end should be connected to ground. In the case of a simple switch, this causes the pin to read HIGH when the switch is open, and LOW when the switch is pressed.

Output
Pins configured as OUTPUT with pinMode() are said to be in a low-impedance state.40 mA
connect OUTPUT pins to other devices with 470Ω or 1k resistors

digitalWrite()



digitalWrite(pin, value)

pin: the pin number

value: HIGH or LOW
here HIGH=1  LOW=0

digitalRead()


评论

热门博文