🚀ELabs IOT Crash Course🚀

🗓️11th Feb 2024
KIIT university logo         Elabs logo

Do you even C bro?🤓

Regular C programming and Arduino's code may seem different but in essence...

(They really aren't!)

In general, it could be broken down to 3 steps

  1. Input⌨️
  2. Process⚙️
  3. Output📺️

Or under the terms of embedded systems/IOT/Arduino

  1. Sensing🌡️
  2. Decision Making⚙️
  3. Actuators🦾

Let's have a side by side comparison....

1. Input ↔️ Sensing

In Regular C

									 
										// Read from stdio and put data to myVar
										scanf("%d", &myVar); 
										// Read formatted data from file
										fscanf(filePtr, "%c %c", &myBuf);
									
								

In Arduino

									
										// Read digital value (0 or 1) from given pin
										bool isBtnPressed = digitalRead(2);
										// Read analog value from given pin
										// 10 bit value 
										//(0 to 1023) for arduino UNO
										int trimVal = analogRead(A0);
									
								
arduino uno along with a button and potentiometer
2. Decision making or processing part is more or less the same!

In Regular C

							 
								int marks;
								scanf("%d", &marks);
								if(marks > 40){
									printf("Pass!\n");
								}
								else{
									printf("Fail!\n");
								}
							
						

In Arduino

							
								int tempSensorVal = analogRead(analogInputPin);
								if (tempSensorVal > 512){
									// Hot! do something here
								}
								else{
									// Normal do something here
								}

							
						
3. Output ↔️ Actuators

In Regular C

									 
										// output to stdio
										printf("your have passed!\n");
										printf("your have failed \n");
										// output to a file
										fprintf(filePtr, "%s\n", "some text");
									
								

In Arduino

									
										// set some pin's value to high/on/1/true
										digitalWrite(outputPin, 1);
										// set some pin's value to low/off/0/false
										digitalWrite(outputPin, 0);
										// generate a analog signal on some pin
										// depends upon PWM resolution supported
										// for example 0 to 255
										// this isn't actually an analog voltage though
										// PWM "simulates" analog voltages
										analogWrite(outputPin, 128);
									
								
arduino uno along with a button and potentiometer

Wait!

One more thing...

🚧structure🚧

In Regular C

						
							// import some libraries on top

							// some global variables declaration
							int main(){
								//main code goes here
								//gets executed sequential
							}
						
					

In Arduino

						
							// import some libraries on top
							// some global variables declaration

							void setup(){
								// code here gets executed once
								// IO declaration also goes here
							}
							void loop(){
								/*after code in setup() gets executed
								code here would get executed again and 
								again until the device is switched off */
							}
						
					

Now, Some end to end examples