Regular C programming and Arduino's code may seem different but in essence...
(They really aren't!)
// Read from stdio and put data to myVar
scanf("%d", &myVar);
// Read formatted data from file
fscanf(filePtr, "%c %c", &myBuf);
// 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);
int marks;
scanf("%d", &marks);
if(marks > 40){
printf("Pass!\n");
}
else{
printf("Fail!\n");
}
int tempSensorVal = analogRead(analogInputPin);
if (tempSensorVal > 512){
// Hot! do something here
}
else{
// Normal do something here
}
// output to stdio
printf("your have passed!\n");
printf("your have failed \n");
// output to a file
fprintf(filePtr, "%s\n", "some text");
// 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);
// import some libraries on top
// some global variables declaration
int main(){
//main code goes here
//gets executed sequential
}
// 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 */
}