By barragan this example code is in the public domain



Download 2,47 Mb.
Sana24.06.2022
Hajmi2,47 Mb.
#698987
Bog'liq
arduino kodlari va natijalari


// C++ code
//
/*
Sweep

by BARRAGAN


This example code is in the public domain.

modified 8 Nov 2013 by Scott Fitzgerald


http://www.arduino.cc/en/Tutorial/Sweep
*/

#include

int pos = 0;

Servo servo_9;

void setup()
{
servo_9.attach(9, 500, 2500);
}

void loop()


{
// sweep the servo from 0 to 180 degrees in steps
// of 1 degrees
for (pos = 0; pos <= 180; pos += 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
for (pos = 180; pos >= 0; pos -= 1) {
// tell servo to go to position in variable 'pos'
servo_9.write(pos);
// wait 15 ms for servo to reach the position
delay(15); // Wait for 15 millisecond(s)
}
}

2. // C++ code
//
/*
AnalogReadSerial
Reads an analog input (potentiometer) on pin 0,
prints the result to the serial monitor.

OPEN THE SERIAL MONITOR TO VIEW THE OUTPUT FROM


THE POTENTIOMETER >>

Attach the center pin of a potentiometer to pin


A0, and the outside pins to +5V and ground.

This example code is in the public domain.


*/

int sensorValue = 0;

void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
}

void loop()


{
// read the input on analog pin 0:
sensorValue = analogRead(A0);
// print out the value you read:
Serial.println(sensorValue);
delay(10); // Delay a little bit to improve simulation performance
}
3. // C++ code
//
/*
Keyboard

Plays a pitch that changes based on a changing


input circuit:
* 3 pushbuttons from +5V to analog in 0 through
3
* 3 10K resistors from analog in 0 through 3 to
ground
* 8-ohm speaker on digital pin 8
*/

int pos = 0;

void setup()
{
pinMode(A0, INPUT);
pinMode(8, OUTPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
}

void loop()


{
// if button press on A0 is detected
if (digitalRead(A0) == HIGH) {
tone(8, 440, 100); // play tone 57 (A4 = 440 Hz)
}
// if button press on A1 is detected
if (digitalRead(A1) == HIGH) {
tone(8, 494, 100); // play tone 59 (B4 = 494 Hz)
}
// if button press on A0 is detected
if (digitalRead(A2) == HIGH) {
tone(8, 523, 100); // play tone 60 (C5 = 523 Hz)
}
delay(10); // Delay a little bit to improve simulation performance
}
4. /*
Melody

Plays a melody

circuit:
* 8-ohm speaker on digital pin 8

created 21 Jan 2010


modified 30 Aug 2011
by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/Tone

*/
#define NOTE_C4 262


#define NOTE_G3 196
#define NOTE_A3 220
#define NOTE_B3 247
#define NOTE_C4 262

// notes in the melody:


int melody[] = {
NOTE_C4, NOTE_G3, NOTE_G3, NOTE_A3, NOTE_G3, 0, NOTE_B3, NOTE_C4
};

// note durations: 4 = quarter note, 8 = eighth note, etc.:


int noteDurations[] = {
4, 8, 8, 4, 4, 4, 4, 4
};

void setup() {


// iterate over the notes of the melody:
for (int thisNote = 0; thisNote < 8; thisNote++) {

// to calculate the note duration, take one second


// divided by the note type.
//e.g. quarter note = 1000 / 4, eighth note = 1000/8, etc.
int noteDuration = 1000 / noteDurations[thisNote];
tone(8, melody[thisNote], noteDuration);

// to distinguish the notes, set a minimum time between them.


// the note's duration + 30% seems to work well:
int pauseBetweenNotes = noteDuration * 1.30;
delay(pauseBetweenNotes);
// stop the tone playing:
noTone(8);
}
}

void loop() {


// no need to repeat the melody.
}
5. // C++ code
//
/*
Multiple tone player

Plays multiple tones on multiple pins in


sequence

circuit:
* 3 8-ohm speaker on digital pins 6, 7, and 8

created 8 March 2010 by Tom Igoe based on a
snippet from Greg Borenstein

This example code is in the public domain.


http://www.arduino.cc/en/Tutorial/Tone4
*/

int pos = 0;

void setup()
{
pinMode(8, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
}

void loop()


{
// turn off tone function for pin 8:
noTone(8);
// play a note on pin 6 for 200 ms:
tone(6, 880, 200); // play tone 69 (A5 = 880 Hz)
delay(200); // Wait for 200 millisecond(s)
// turn off tone function for pin 6:
noTone(6);
// play a note on pin 7 for 500 ms:
tone(7, 988, 500); // play tone 71 (B5 = 988 Hz)
delay(500); // Wait for 500 millisecond(s)
// turn off tone function for pin 7:
noTone(7);
// play a note on pin 8 for 300 ms:
tone(8, 1047, 300); // play tone 72 (C6 = 1047 Hz)
delay(300); // Wait for 300 millisecond(s)
}
6. // C++ code
//
/*
Pitch follower

Plays a pitch that changes based on a changing


analog input

circuit:
* 8-ohm speaker on digital pin 9


* photoresistor on analog 0 to 5V
* 4.7K resistor on analog 0 to ground

created 21 Jan 2010


modified 31 May 2012 by Tom Igoe, with
suggestion from Michael Flynn

This example code is in the public domain.


http://www.arduino.cc/en/Tutorial/Tone2
*/

int sensorReading = 0;

void setup()
{
pinMode(A0, INPUT);
Serial.begin(9600);
pinMode(9, OUTPUT);
}

void loop()


{
// read the sensor
sensorReading = analogRead(A0);
// print the sensor reading so you know its range
Serial.println(sensorReading);
// map the sensor reading to a range for the
// speaker
tone(9, 440 * pow(2.0, (constrain(int(map(sensorReading, 0, 1023, 36, 84)), 35, 127) - 57) / 12.0), 1000);
delay(10); // Delay a little bit to improve simulation performance
}
7. // C++ code
//
/*
Ping))) Sensor

This sketch reads a PING))) ultrasonic


rangefinder and returns the distance to the
closest object in range. To do this, it sends a
pulse to the sensor to initiate a reading, then
listens for a pulse to return. The length of
the returning pulse is proportional to the
distance of the object from the sensor.

The circuit:


* +V connection of the PING))) attached to +5V
* GND connection attached to ground
* SIG connection attached to digital pin 7

http://www.arduino.cc/en/Tutorial/Ping

This example code is in the public domain.
*/

int inches = 0;

int cm = 0;

long readUltrasonicDistance(int triggerPin, int echoPin)


{
pinMode(triggerPin, OUTPUT); // Clear the trigger
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
// Sets the trigger pin to HIGH state for 10 microseconds
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
pinMode(echoPin, INPUT);
// Reads the echo pin, and returns the sound wave travel time in microseconds
return pulseIn(echoPin, HIGH);
}

void setup()


{
Serial.begin(9600);
}

void loop()


{
// measure the ping time in cm
cm = 0.01723 * readUltrasonicDistance(7, 7);
// convert to inches by dividing by 2.54
inches = (cm / 2.54);
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.println("cm");
delay(100); // Wait for 100 millisecond(s)
}
9. #include

#define PIN 2 // input pin Neopixel is attached to

#define NUMPIXELS 12 // number of neopixels in strip

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);

int delayval = 100; // timing delay in milliseconds

int redColor = 0;


int greenColor = 0;
int blueColor = 0;

void setup() {


// Initialize the NeoPixel library.
pixels.begin();
}

void loop() {


setColor();

for (int i=0; i < NUMPIXELS; i++) {


// pixels.Color takes RGB values, from 0,0,0 up to 255,255,255
pixels.setPixelColor(i, pixels.Color(redColor, greenColor, blueColor));

// This sends the updated pixel color to the hardware.


pixels.show();

// Delay for a period of time (in milliseconds).


delay(delayval);
}
}

// setColor()


// picks random values to set for RGB
void setColor(){
redColor = random(0, 255);
greenColor = random(0,255);
blueColor = random(0, 255);
}
10. // C++ code
//
/*
LiquidCrystal Library - Hello World

Demonstrates the use of a 16x2 LCD display.


The LiquidCrystal library works with all LCD
displays that are compatible with the Hitachi
HD44780 driver. There are many of them out
there, and you can usually tell them by the
16-pin interface.

This sketch prints "Hello World!" to the LCD


and shows the time.

The circuit:


* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)

Library originally added 18 Apr 2008 by David


A. Mellis
library modified 5 Jul 2009 by Limor Fried
(http://www.ladyada.net)
example added 9 Jul 2009 by Tom Igoe
modified 22 Nov 2010 by Tom Igoe

This example code is in the public domain.

http://www.arduino.cc/en/Tutorial/LiquidCrystal
*/

#include

int seconds = 0;

LiquidCrystal lcd_1(12, 11, 5, 4, 3, 2);

void setup()
{
lcd_1.begin(16, 2); // Set up the number of columns and rows on the LCD.

// Print a message to the LCD.


lcd_1.print("hello world!");
}

void loop()


{
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting
// begins with 0):
lcd_1.setCursor(0, 1);
// print the number of seconds since reset:
lcd_1.print(seconds);
delay(1000); // Wait for 1000 millisecond(s)
seconds += 1;
}
11. // C++ code
//
#include

int seconds = 0;

Adafruit_LiquidCrystal lcd_1(0);

void setup()


{
lcd_1.begin(16, 2);

lcd_1.print("hello world");


}

void loop()


{
lcd_1.setCursor(0, 1);
lcd_1.print(seconds);
lcd_1.setBacklight(1);
delay(500); // Wait for 500 millisecond(s)
lcd_1.setBacklight(0);
delay(500); // Wait for 500 millisecond(s)
seconds += 1;
}

12. // C++ code
//
int moisture = 0;

void setup()


{
pinMode(A0, OUTPUT);
pinMode(A1, INPUT);
Serial.begin(9600);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
}

void loop()


{
// Apply power to the soil moisture sensor
digitalWrite(A0, HIGH);
delay(10); // Wait for 10 millisecond(s)
moisture = analogRead(A1);
// Turn off the sensor to reduce metal corrosion
// over time
digitalWrite(A0, LOW);
Serial.println(moisture);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
if (moisture < 200) {
digitalWrite(12, HIGH);
} else {
if (moisture < 400) {
digitalWrite(11, HIGH);
} else {
if (moisture < 600) {
digitalWrite(10, HIGH);
} else {
if (moisture < 800) {
digitalWrite(9, HIGH);
} else {
digitalWrite(8, HIGH);
}
}
}
}
delay(100); // Wait for 100 millisecond(s)
}
Download 2,47 Mb.

Do'stlaringiz bilan baham:




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish