This document contains code snippets for controlling an LED or reading sensor values using an Arduino board. The snippets demonstrate basic digital input/output functions like turning an LED on and off, fading an LED brightness over time, reading the state of a push button to control an LED, reading analog sensor values to control LED brightness, receiving serial data to set LED brightness, and sending mouse position data over serial.
Top profile Call Girls In Meerut [ 7014168258 ] Call Me For Genuine Models We...
Arduino Workshop 2011.05.31
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26. void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
27. void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}
28.
29.
30. int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(9, OUTPUT);
}
void loop() {
// set the brightness of pin 9:
analogWrite(9, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
delay(30);
}
31.
32.
33.
34.
35. const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop(){
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
}
36.
37.
38.
39.
40. const int sensorPin = A0;
const int ledPin = 9;
void setup() {
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
int sensorValue = analogRead(sensorPin);
// scale from 0-1023 to 0-255
int intensity = map(sensorValue, 0, 1023, 0, 255);
// sets the value
analogWrite(ledPin, intensity);
}
41.
42.
43.
44. const int ledPin = 9;
void setup() {
// initialize the serial communication:
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
}
void loop() {
byte brightness;
// check if data has been sent from the computer:
if (Serial.available()) {
// read the most recent byte (0-255)
brightness = Serial.read();
// set the brightness of the LED:
analogWrite(ledPin, brightness);
}
}
45. import processing.serial.*;
Serial port;
void setup() {
size(256, 150);
// Print available serial ports
println(Serial.list());
port = new Serial(this, "COM1", 9600);
}
void draw() {
// draw a gradient from black to white
...
// send the current X-position of the mouse
port.write(mouseX);
}