Well , here i will continue on with programming , or at least the basics of it .
Here is a screenshot of the Arduino Genuino software with which one can programme a microcontroller chip .
As you can see it uses C++ as the programming language , so it is quite simple to get the hang of it .
The programme you see ( or at least the part of it ) is a setup for 4 LEDs as running lights .
As you can also see , we have commands such as pinMode . pinMode sets the desired pin of the chip as either an INPUT or an OUTPUT pin , hence the I/O pin designation .
int stands for integer , and in this case we use it as a means of attributing a certain "name" to the desired pin , and also to define what kind of data can we see on these pins . In this case a 0 or 1 which are whole numbers .
The other commands i will use are :
-digitalWrite()
-digitalRead()
-analogRead()
Digital write and read are commands which either set a desired pin to either HIGH (1) state or LOW(0) state , or read the state the pin is , and we can use that info later in the programme .
analog read reads the state of one of the 5 analog pins . Again as with digitalRead we can use this data later in the programme .
Now , the functions im expecting to use are :
-IF-Else
-FOR
-DO-While
-Switch
IF function works as following :
if ( condition )
{
Programme A
}
else
{
Programme B
}
We have a condition which for example would be engine RPM . IF the RPM range is between a 1000 and 5000 RPM , or in other words if the condition is TRUE , it will run the Programme A.
If the condition is not met , it skips over Programme A and goes thru Programme B
FOR works with the same principles as IF , but is better suited for the task of engine menagement .
In the FOR function we have a varibale ( integer in this case ) , a test ( condition) and an increment .
void loop()
{
for (int i=0; i <= 255; i++){
analogWrite(PWMpin, i);
delay(10);
}
The reason this function is better suited is that it is used in repetitive tasks , and can be done quicker than the IF. Also , for having the useful increment , it can also be a safeguard to make sure everything is working as supposed .
DO-While , as its name sais , enables a certain programme to be run WHILE the conditions are TRUE . This function also favors repetitive tasks .
do
{
delay(50); // wait for sensors to stabilize
x = readSensors(); // check the sensors
} while (x < 100);
Switch is a function which enables an x amount of programmes to be run according to the state of the condition .
switch (var) {
case 1:
//do something when var equals 1
break;
case 2:
//do something when var equals 2
break;
default:
// if nothing else matches, do the default
// default is optional
break;
}
For example , if engine cylinder head temperature is below 150.C but is above 125.C , run programme A . If it is above 150.C run programme B and if its below 125.C run programme C.
This enables me to write the code in much more detail than with an IF function , even tho it is the easiest one to use .
Because , to do the same task a single switch function can do , i would need 4 IF functions , branched inside eachother for the same task .