![](https://universalassignment.com/wp-content/uploads/2022/03/Struggling-with-Assignment-1.png)
KIT406 Answer Sheet
Tutorial 1 :
#define BUZ_PIN 11 // define the pin number of the Buzzer module
#define DO 256 // define the frequency(DO)
#define RE 288 // define the frequency(RE)
#define MI 320 // define the frequency(MI)
#define FA 341 // define the frequency(FA)
#define SO 384 // define the frequency(SO)
#define LA 427 // define the frequency(LA)
#define TI 480 // define the frequency(TI)
void setup() {
// note that no setup is required
}
void loop() {
tone(BUZ_PIN, DO*2, 100); // play the “DO” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, RE*2, 100); // play the “RE” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, MI*2, 100); // play the “MI” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, FA*2, 100); // play the “FA” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, SO*2, 100); // play the “SO” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, LA*2, 100); // play the “LA” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, TI*2, 100); // play the “TI” frequency
delay(1000); // delay 1 second
tone(BUZ_PIN, DO*4, 100); // play the “DO” frequency
delay(3000); // delay 3 second
}
Activity:
tone(BUZ_PIN, MI*2, 100); // play the “MI” frequency
delay(1000); // delay 0.5 second
tone(BUZ_PIN, RE*2, 100); // play the “RE” frequency
delay(1000); // delay 0.5 second
tone(BUZ_PIN, DO*2, 100); // play the “DO” frequency
delay(1000); // delay 0.5 second
Tutorial 2:
*
Ultrasonic sensor Pins:
VCC: +5VDC
Trig : Trigger (INPUT) – Pin11
Echo: Echo (OUTPUT) – Pin 12
GND: GND
*/
int trigPin = 11; // Trigger
int echoPin = 12; // Echo
long duration, cm, inches;
void setup() {
//Serial Port begin
Serial.begin (9600);
//Define inputs and outputs
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Tutorial 3:
const int ledPin = 13;
int ledState = LOW;
unsigned long previousMillis = 0;
const unsigned long gap = 50;
const unsigned long interval = 500;
void setup()
{
pinMode(ledPin, OUTPUT);
}
void loop()
{
unsigned long currentMillis = millis();
if(currentMillis – previousMillis > gap)
{
previousMillis = currentMillis;
if (ledState == LOW)
ledState = HIGH;
else
ledState = LOW;
digitalWrite(ledPin, ledState);
}
}
Tutorial 4.
float sum = 0;
int count = 0;
void setup() {
// setup serial comms
if(
Serial.begin = 57600)
pinMode(A0, INPUT);
Serial.println(“Bright”);
}else
Serial.println(“Dark”);
void loop() {
sum += analogRead(A0);
count++;
if(count > 100) {
Serial.println(sum / 100.0);
count = 0;
sum = 0;
}
delay(100);
}
Tutorial 5.
int NEAR = 0;
int FAR = 1;
int TOOFAR = 2;
loop() {
if(NEAR == 0) {
if(delay = 10000) {
Serial.println(“10, Warning”);
}
}
else if(FAR == 1) {
if(delay = 10000) {
Serial.println(“20, Warning”);
}
}
else if(TOOFAR == 2) {
if(delay = 10000) {
Serial.println(“30, Warning”);
}
}
}
Tutorial 6.
/* an ultrasonic alarm system using */
//Firstly the connections of ultrasonic Sensor.Connect +5v and GND normally and trigger pin to 12 & echo pin to 13.
#define trigPin 12
#define echoPin 13
int Buzzer = 8; // Connect buzzer pin to 8
int ledPin= 6; //Connect LEd pin to 6
int duration, distance; //to measure the distance and time taken
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(Buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
}
void loop() {
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (duration/2) / 29.1;
//when distance is greater than or equal to 200 OR less than or equal to 0,the buzzer and LED are off
if (distance >= 300 || distance <= 900)
{
Serial.println(“object detected”);
digitalWrite(Buzzer,LOW);
digitalWrite(ledPin,GREEN);
}
else if (distance < 300 )
{
Serial.println(“object detected \n”);
tone(Buzzer,200);
digitalWrite(ledPin,RED);
}
}
Tutorial 7.
int red_light_pin= 11;
int green_light_pin = 10;
int blue_light_pin = 9;
void setup() {
pinMode(red_light_pin, OUTPUT);
pinMode(green_light_pin, OUTPUT);
pinMode(blue_light_pin, OUTPUT);
}
void loop() {
RGB_color(255, 0, 0); // Red
delay(1000);
RGB_color(0, 255, 0); // Green
delay(1000);
RGB_color(0, 0, 255); // Blue
delay(1000);
RGB_color(255, 255, 125); // Raspberry
delay(1000);
RGB_color(0, 255, 255); // Cyan
delay(1000);
RGB_color(255, 0, 255); // Magenta
delay(1000);
RGB_color(255, 255, 0); // Yellow
delay(1000);
RGB_color(255, 255, 255); // White
delay(1000);
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(red_light_pin, red_light_value);
analogWrite(green_light_pin, green_light_value);
analogWrite(blue_light_pin, blue_light_value);
}
Tutorial 8 a.
void setup()
{
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
}
void loop()
{
analogWrite(11, 128);
analogWrite(10, 0);
analogWrite(9, 0);
delay(3000);
analogWrite(11, 255);
analogWrite(10, 255);
analogWrite(9, 102);
delay(3000);
}
Tutorial 8 b.
int m_ctrl;
void setup() {
pinMode(pm1, OUTPUT);
pinMode(pm2, OUTPUT);
}
void loop() {
m_ctrl = analogRead(joystick);
m_ctrl >>= 1;
if(m_ctrl > 255){
digitalWrite(pm2, 0);
analogWrite(pm1, (m_ctrl – 256));
}
else
if(m_ctrl < 255){
digitalWrite(pm1, 0);
analogWrite(pm2, (255 – m_ctrl));
}
else{
digitalWrite(pm1, 0);
digitalWrite(pm2, 0);
}
}
Tutorial 9.
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(57600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“Initializing SD card…”);
if (!SD.begin(53)) { //CS pin is 53
Serial.println(“initialization failed!”);
while (1);
}
Serial.println(“initialization done.”);
if(SD.exists(“test.txt”) {
SD.remove(“test.txt”);
}
}
void loop() {
if(Serial.available()){
n = Serial.read();
Serial.print(“You typed: ” );
Serial.println(n);
If(n==0){
myFile = SD.open(“test.txt”);
if (myFile) {
Serial.println(“test.txt:”);
// read from the file until there’s nothing else in it:
while (myFile.available()) {
Serial.write(myFile.read());
}
// close the file:
myFile.close();
}
}else if(n==1){
Serial.println(“error opening test.txt”);
}else{
Serial.println(“error opening test.txt”);
}
}
delay(1000);
}
Tutorial 10 a:
uint_t num = 0xAA;
led.setLED(num);
Tutorial 10 b:
#include “RL_LEDArray.h”
LED led;
uint_t num = 0x01;
void setup()
{
led.AllOff();
Serial.begin(57600);
}
void loop()
{
led.setLED(num);
// This function writes the binary value into the LED Bar directly
Serial.println(num, BIN);
num = num << 1;
if(num == 0) num = 0x01;
delay(1000);
}
![Order Now](https://universalassignment.com/wp-content/uploads/2022/04/Order-Now-Assignment.png)