Arduino Example
Arduino Example
/*
keyestudio super learning kit
Project 3
pwm
http//www.keyestudio.com
*/
int ledPin = 10;
void setup() {
pinMode(ledPin,OUTPUT);
}
void loop(){
for (int value = 0 ; value < 255; value=value+1){
analogWrite(ledPin, value);
delay(5);
}
for (int value = 255; value >0; value=value-1){
analogWrite(ledPin, value);
delay(5);
}
}
Project - 4
/*
keyestudio super learning kit
Project 4
traffic light
http//www.keyestudio.com
*/
int redled =10; // initialize digital pin 10.
int yellowled =7; // initialize digital pin 7.
int blueled =4; // initialize digital pin 4.
void setup()
{
pinMode(redled, OUTPUT);// set the pin with red LED as “output”
pinMode(yellowled, OUTPUT); // set the pin with yellow LED as “output”
pinMode(blueled, OUTPUT); // set the pin with blue LED as “output”
}
void loop()
{
digitalWrite(blueled, HIGH);//// turn on blue LED
delay(5000);// wait 5 seconds
digitalWrite(blueled, LOW); // turn off blue LED
for(int i=0;i<3;i++)// blinks for 3 times
{
delay(500);// wait 0.5 second
digitalWrite(yellowled, HIGH);// turn on yellow LED
delay(500);// wait 0.5 second
digitalWrite(yellowled, LOW);// turn off yellow LED
}
delay(500);// wait 0.5 second
digitalWrite(redled, HIGH);// turn on red LED
delay(5000);// wait 5 second
digitalWrite(redled, LOW);// turn off red LED
}
Project – 5
/*
keyestudio super learning kit
Project 5
LED Chasing Effect
http//www.keyestudio.com
*/
int BASE = 2 ; // the I/O pin for the first LED
int NUM = 6; // number of LEDs
void setup()
{
for (int i = BASE; i < BASE + NUM; i ++)
{
pinMode(i, OUTPUT); // set I/O pins as output
}
}
void loop()
{
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, LOW); // set I/O pins as “low”, turn off LEDs one
by one.
delay(200); // delay
}
for (int i = BASE; i < BASE + NUM; i ++)
{
digitalWrite(i, HIGH); // set I/O pins as “high”, turn on LEDs one
by one
delay(200); // delay
}
}
Project – 6
/*
keyestudio super learning kit
Project 6
Button
http//www.keyestudio.com
*/
int ledpin=11;// initialize pin 11
int inpin=7;// initialize pin 7
int val;// define val
void setup()
{
pinMode(ledpin,OUTPUT);// set LED pin as “output”
pinMode(inpin,INPUT);// set button pin as “input”
}
void loop()
{
val=digitalRead(inpin);// read the level value of pin 7 and assign if to
val
if(val==LOW)// check if the button is pressed, if yes, turn on the LED
{ digitalWrite(ledpin,LOW);}
else
{ digitalWrite(ledpin,HIGH);}
}
Project – 7
/*
keyestudio super learning kit
Project 7
Active Buzzer
http//www.keyestudio.com
*/
int buzzer=8;// initialize digital IO pin that controls the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set pin mode as “output”
}
void loop()
{
digitalWrite(buzzer, HIGH); // produce sound
}
Project – 8
/*
keyestudio super learning kit
Project 8
Passive Buzzer
http//www.keyestudio.com
*/
int buzzer=8;// select digital IO pin for the buzzer
void setup()
{
pinMode(buzzer,OUTPUT);// set digital IO pin pattern, OUTPUT to be output
}
void loop()
{ unsigned int i,j;//define variable
while(1)
{ for(i=0;i<80;i++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(1);//delay1ms
digitalWrite(buzzer,LOW);//not sound
delay(1);//ms delay
}
for(j=0;j<100;j++)// output a frequency sound
{ digitalWrite(buzzer,HIGH);// sound
delay(2);//2ms delay
digitalWrite(buzzer,LOW);//not sound
delay(2);//2ms delay
}}}
Project – 9
int redpin = 11; //select the pin for the red LED
int bluepin =10; // select the pin for the blue LED
int greenpin =9;// select the pin for the green LED
int val;
void setup() {
pinMode(redpin, OUTPUT);
pinMode(bluepin, OUTPUT);
pinMode(greenpin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
for(val=255; val>0; val--)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(1);
}
for(val=0; val<255; val++)
{
analogWrite(11, val);
analogWrite(10, 255-val);
analogWrite(9, 128-val);
delay(1);
}
Serial.println(val, DEC);
}
Project 10
/*
keyestudio super learning kit
Project 10
Photo Resistor
http//www.keyestudio.com
*/
int potpin=0;// initialize analog pin 0, connected with photovaristor
int ledpin=11;// initialize digital pin 11,
int val=0;// initialize variable va
void setup()
{
pinMode(ledpin,OUTPUT);// set digital pin 11 as “output”
Serial.begin(9600);// set baud rate at “9600”
}
void loop()
{
val=analogRead(potpin);// read the value of the sensor and assign it to
val
Serial.println(val);// display the value of val
analogWrite(ledpin,val/4);// set up brightness(maximum value 255)
delay(10);// wait for 0.01
}
Project 11
/*
keyestudio super learning kit
Project 13
Tilt Switch
http//www.keyestudio.com
*/
void setup()
{
pinMode(8,OUTPUT);// set digital pin 8 as “output”
}
void loop()
{
int i;// define variable i
while(1)
{
i=analogRead(5);// read the voltage value of analog pin 5
if(i>512)// if larger that 512(2.5V)
{
digitalWrite(8,LOW);// turn on LED
}
else// otherwise
{
digitalWrite(8,HIGH);// turn off LED
} } }
Push on/off
#include <stdio.h>
#include <stdlib.h>
void setup() {
pinMode(button,INPUT); // define button as an input
pinMode(led,OUTPUT); // define LED as an output
digitalWrite(led,LOW); // turn output off just in case
}
void loop() {
if (digitalRead(button)==HIGH){ // if button is pressed
if (ledflag==0) { // and the status flag is LOW
ledflag=1; // make status flag HIGH
digitalWrite(led,HIGH); // and turn on the LED
} //
else { // otherwise...
ledflag=0; // make status flag LOW
digitalWrite(led,LOW); // and turn off the LED
}
delay(1000); // wait a sec for the
} } // hardware to stabilize
/*
AnalogReadSerial
https://www.arduino.cc/en/Tutorial/BuiltInExamples/AnalogReadSerial
*/
Then define the digital pin in which the DHT11 is connected to.
#define DHTPIN 2 // Digital pin connected to the DHT sensor
Now define the type of DHT Sensor. Since we're using a DHT11 sensor, we can
write like this.
#define DHTTYPE DHT11 // DHT 11
dht.begin();
}
Now, inside the void loop function, let's measure the readings.
Reading temperature or humidity takes about 250 milliseconds! Sensor
readings may also be up to 2 seconds 'old' (its a very slow sensor).
float h = dht.readHumidity(); // read humidity
Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
while(1)
It is an infinite loop which will run till a break statement is issued explicitly. Interestingly not
while(1) but any integer which is non-zero will give a similar effect as while(1). Therefore,
while(1), while(2) or while(-255), all will give infinite loop only.
while(1) or while(any non-zero integer)
{
// loop runs infinitely
}
A simple usage of while(1) can be in the Client-Server program. In the program, the server
runs in an infinite while loop to receive the packets sent from the clients.
But practically, it is not advisable to use while(1) in real-world because it increases the CPU
usage and also blocks the code i.e one cannot come out from the while(1) until the program is
closed manually. while(1) can be used at a place where condition needs to be true always.
#include <stdio.h>
int main()
{
int i = 0;
while (1) {
printf("%d\n", ++i);
if (i == 5)
// out of loop
return 0;
Output
1
2
3
4
5
while(0)
It is opposite of while(1). It means condition will always be false and thus code in while will
never get executed.
while(0)
{
// loop does not run
}
// C program to illustrate while(0)
#include<stdio.h>
int main()
int i = 0, flag=0;
while ( 0 )
flag++;
if (i == 5)
break;
if (flag==0)
return 0;
Output
Didn't execute the loop!
I have been trying to exit from the while(1) loop by getting a value from the sensor and then run
the code outside while(1).But the problem is that it does not exit. How may I fix this problem?
void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
}
void loop() {
int sensor;
while(1)
{
sensor = digitalRead(2);
if(sensor == 0)
{
Serial.println("inside while loop");
}
break;
}
Serial.println("Break......");
}
correction
void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
}
void loop() {
bool cond = true;
int sensor;
while(cond)
{
sensor = digitalRead(2);
if(sensor == 0)
{
Serial.println("inside while loop");
}
cond = false;
}
// like this you can used while inside the void loop()
}
void setup() {
pinMode(2,INPUT);
Serial.begin(9600);
int sensor;
}
void loop() {
while(1)
{
sensor = digitalRead(2);
if(sensor == 1)
break;
else
{Serial.println("inside while loop"); delay(500);}
}
Serial.println("Break......");
Delay(500);
}
int sensorPin =A0 ; // define analog port A0
int value = 0; //set value to 0
void setup()
{
Serial.begin(9600); //set the baud rate to 9600
}
void loop()
{
value = analogRead(sensorPin); //set the value as the value read from A0
Serial.println(value, DEC); //print the value and line wrap
delay(200); //delay 0.2S
}
Microphone sensor
1. Digital Write
2. Analog Write(project-3)
3. Digital Read (button-proj6 & toggle switch)
4. Analog Read, Serial Read and Traffic(proj5)
5. LED Chasing(proj5), Active Bazar(proj7) & Passive Bazar (Proj8)