Author Archives: Sajjad Baghaee

ARDUINO UNO Project 05: Mood Cue

Published by:

Ingredients :

1- potentiometer       #1

2- capacitor 100µF    #2

3- Servo motor         #1

 

BUILD THE CIRCUIT

ARDUINO UNO Project 05: Mood Cue

 

 

Code:

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

}

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

ARDUINO UNO Project 04: Color Mixing Lamp

Published by:

Ingredients :

1- RGB LED                   #1

2- Phototransistor         #3

3- Resistor   220Ω         #3

4- Resistor   10KΩ         #3

5- Gel Red                    #1

6- Gel Green                 #1

7- Gel Blue                    #1

8- Wire

BUILD THE CIRCUIT

ARDUINO UNO Project 04: Color Mixing Lamp

ARDUINO UNO Project 04: Color Mixing Lamp

ARDUINO UNO Project 04: Color Mixing Lamp

ARDUINO UNO Project 04: Color Mixing Lamp

Code:
const int greenLEDPin=9;
const int blueLEDPin=10;
const int redLEDPin=11;

const int redSensorPin=A0;
const int greenSensorPin=A1;
const int blueSensorPin=A2;

int redValue=0;
int greenValue=0;
int blueValue=0;

int redSensorValue=0;
int greenSensorValue=0;
int blueSensorValue=0;

void setup() {
Serial.begin(9600);

pinMode(greenLEDPin,OUTPUT);
pinMode(blueLEDPin,OUTPUT);
pinMode(redLEDPin,OUTPUT);
}

void loop() {
redSensorValue=analogRead(redSensorPin);
delay(5);
greenSensorValue=analogRead(greenSensorPin);
delay(5);
blueSensorValue=analogRead(blueSensorPin);

Serial.print(“Raw Sensor Value \t red: “);
Serial.print(redSensorValue);
Serial.print(” \t green: “);
Serial.print(greenSensorValue);
Serial.print(” \t blue: “);
Serial.println(blueSensorValue);

redValue=redSensorValue/4;
greenValue=greenSensorValue/4;
blueValue=blueSensorValue/4;

Serial.print(“Maped Sensor Value \t red: “);
Serial.print(redValue);
Serial.print(” \t green: “);
Serial.print(greenValue);
Serial.print(” \t blue: “);
Serial.println(blueValue);

analogWrite(redLEDPin,redValue);
analogWrite(greenLEDPin,greenValue);
analogWrite(blueLEDPin,blueValue);

}

ARDUINO UNO Project 03: Love-O-Meter

Published by:

Ingredients :

1- LED – RED                            # 3

2- Tempererature Sensor          #1

3- Wire

4- Resistor   220Ω                     #3

 

BUILD THE CIRCUIT

ARDUINO UNO Project 03 Love O Meter

ARDUINO UNO Project 03 Love O Meter

ARDUINO UNO Project 03 Love O Meter

 

Code:
const int SensorPin=A0;
const float baselineTemp=24.0;

void setup() {
for (int pinNum=3; pinNum<=5;pinNum++ ){
pinMode(pinNum, OUTPUT);
}
Serial.begin(9600);
}

void loop() {
int SensorValue=analogRead(SensorPin);
float voltage=(SensorValue/1024.0)*5.0;
float temperature=(voltage-.5)*100;

if(temperature<baselineTemp+2){
for (int pinNum=3; pinNum<=5;pinNum++ ){
digitalWrite(pinNum,LOW);}
} else if(temperature>= baselineTemp+2 && temperature<= baselineTemp+4 ){
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
}else if(temperature> baselineTemp+4 && temperature<= baselineTemp+6 ){
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
}else if (temperature> baselineTemp+6 ){
for (int pinNum=3; pinNum<=5;pinNum++ ){
digitalWrite(pinNum,HIGH);}
}
Serial.print(“Base Line Temp: “);
Serial.print(baselineTemp);
Serial.print(” , Sensor Value: “);
Serial.print(SensorValue);
Serial.print(” , Volts: “);
Serial.print(voltage);
Serial.print(” , Temperature: “);
Serial.println(temperature);

delay(250);

}

 

ARDUINO UNO Project 02: Spaceship Interface

Published by:

Ingredients :

1- LED – RED                      # 2

2- LED – GREEN                 # 1

3- Switch                             #1

4- Wire

5- Resistor   220Ω              #3

6- Resistor   10KΩ              #1

BUILD THE CIRCUIT

ARDUINO UNO Project 02: Spaceship Interface

ARDUINO UNO Project 02: Spaceship Interface

ARDUINO UNO Project 02: Spaceship Interface

 

 

Code:

int KeyState=0;
void setup() {
// put your setup code here, to run once:
pinMode(2, INPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}

void loop() {
// put your main code here, to run repeatedly:
KeyState=digitalRead(2);
if (KeyState==0){
digitalWrite(3,HIGH);
digitalWrite(4,LOW);
digitalWrite(5,LOW);
}else{
digitalWrite(3,LOW);
digitalWrite(4,HIGH);
digitalWrite(5,LOW);

delay(250);

digitalWrite(4,LOW);
digitalWrite(5,HIGH);

delay(250);

}

}

 

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

Published by:

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 :

Published by:

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 – 15 – Extract a character from an array + compare it with a character :

Published by:

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 :

Published by:

Code:
—————————

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

int main()
{

float agrade,bgrade,cgrade;
printf(“Please enter you 3 grades. \n”);
scanf(” %f”, &agrade); // You should not use \n in scanf. !!!!
scanf(” %f”, &bgrade);
scanf(” %f”, &cgrade);
float averagegarde = (agrade+bgrade+cgrade)/3;
printf(“Your average grade is : %.2f \n”,averagegarde);
if (averagegarde>=90){ // Use of else if
printf(“Your FINAL grade is A. \n”);
} else if (averagegarde>= 80) {
printf(“Your FINAL grade is B. \n”);
} else if (averagegarde>= 70){
printf(“Your FINAL grade is C. \n”);
} 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;

}