# Tag Archives: C Programming

## ARDUINO UNO Project 05: Mood Cue

Ingredients :

1- potentiometer       #1

2- capacitor 100µF    #2

3- Servo motor         #1

BUILD THE CIRCUIT

Code:

#include <Servo.h>
Servo TestServo;
int const potPin=A0;
int potValue;
int angle;
void setup() {
TestServo.attach(9);
Serial.begin(9600);

}

void loop() {
Serial.print(“potValue: “);
Serial.print(potValue);
angle=map(potValue, 0,1023,0,179);
Serial.print(“,angle: “);
Serial.println(angle);
TestServo.write(angle);
delay(15);
}

## C Programming Tutorial – 18 – ++ in Left and Right:

Note:

• // ++ in left first add 1 to viable and then calculate the equation.
• // ++ in right first calculate the equation and then add 1 to viable .

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int IncResult, increment=5 , constant=4;

// ++ in left first add 1 to increment and then calculate the equation.
IncResult = ++increment *constant;
printf(“increment = %d and constant= %d so IncResult= %d \n”,increment ,constant ,IncResult );

// since we defined the variable previously so we do not need to define them again.

IncResult=0 , increment=5 , constant=4;

// ++ in right first calculate the equation and then add 1 to increment

IncResult = increment++ *constant;
printf(“increment = %d and constant= %d so IncResult= %d \n”,increment ,constant ,IncResult );

return 0;

}

## C Programming Tutorial – 17 – Short form of if + add a string :

Note:

• //Example for Short form of if+add a string

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int friends;
printf(“How many friends do you have ? \n”);
scanf(” %d”, &friends);

// If you have 0 or 2 or 3 or … friends it shows s else if you have 1 friend you will not see s.
printf(“You have %d friend%s \n”, friends ,(friends !=1 ) ? “s” : “”);

return 0;

}

## C Programming Tutorial – 16 – Short form of if :

Note:

• //Short form of if:   ( ….. ) ?   ….. : ….. ;

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int num=11;
(num>10) ? printf(“It is grater that 10 \n”) :   printf(“It is less than 10 \n”);

return 0;

}

## C Programming Tutorial – 15 – Extract a character from an array + compare it with a character :

Note:

• //lastname[3] : Extract the 4th (not the 3rd) character of array lastname.
• //lastname[3] > ‘M’   : compare the 4th character with character M.

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

char lastname[20];
printf(“Enter your last name ? \n”);
scanf(” %s”, lastname);
if (lastname[3] > ‘M’) { // character extract and character comparison.
printf(“You are in team Blue \n”);
} else {
printf(“You are in team Red \n”);
}

return 0;

}

## C Programming Tutorial – 14 – if – else if :

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

scanf(” %f”, &agrade); // You should not use \n in scanf. !!!!
if (averagegarde>=90){ // Use of else if
} else if (averagegarde>= 80) {
} else if (averagegarde>= 70){
} else {
printf(“You could not pass the course ! \n”);
}
if ((agrade>=90) && (bgrade>=80) && (cgrade>=70)){       // In this part we compare more 1 values and use logic syntax like && ||
printf(“You can enter to Final EXAM.\n”);
} else
{
printf(“You cannot enter to Final exam !\n”);}

return 0;

}

## C Programming Tutorial – 13 – if :

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int Yourage;
char gender;
printf(“How old are you?\n”);
scanf(” %d”,&Yourage);
scanf(” %c”,&gender);

if(Yourage>=18)
{
if(gender == ‘m’) // compare the char value
{
printf(“welcome dude !\n”);
}

if(gender == ‘f’)
{
}

}

if(Yourage <18)
{
printf(“You can not visit this website.\n”);
}

return 0;

}

## C Programming Tutorial – 12 – change the property of a variable:

Note:

• //Use (float) or (int) or … before any variable to change that variable to float or int or …:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int PriceOfPopcorn=100,sales=20,WorkDay=7;
float profitPerday;
profitPerday = ((float)PriceOfPopcorn*(float)sales)/(float)WorkDay;
printf(“profit per day is = \$%.2f\n”,profitPerday);

return 0;

}

## C Programming Tutorial – 10 – define multiple variable at the same time :

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

float aa,bb,cc, balance;                                          // we can define multiple variable at the same time
aa=bb=cc=100.00;                                               // we can give a value to multiple variable at the same time
printf(“aa=%f , bb=%f ,cc=%f\n”, aa,bb,cc);

return 0;

}

## C Programming Tutorial – 9 – int or float ? :

Note:

• // int just shows the integer part of the number
• // float shows the integer and decimal part of the number:

Code:
—————————

#include <stdio.h>
#include <stdlib.h>

int main()
{

int a=86;                                            // Int just shows the integer part of the number
int b=21;
printf(“%d\n”, a/b);
float c=86.0;                                     // float shows the integer and decimal part of the number
float d=21.0;
printf(“%f\n”, c/d);

return 0;

}