Sunteți pe pagina 1din 15

/*

AnalogReadSerial

Reads an analog input on pin 0, prints the result to the Serial Monitor.

Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).

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.

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

*/

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

int sensorValue = analogRead(A0);

// print out the value you read:

Serial.println(sensorValue);

delay(1); // delay in between reads for stability

}
/*

DigitalReadSerial

Reads a digital input on pin 2, prints the result to the Serial Monitor

This example code is in the public domain.

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

*/

// digital pin 2 has a pushbutton attached to it. Give it a name:

int pushButton = 2;

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// make the pushbutton's pin an input:

pinMode(pushButton, INPUT);

// the loop routine runs over and over again forever:

void loop() {

// read the input pin:

int buttonState = digitalRead(pushButton);

// print out the state of the button:

Serial.println(buttonState);

delay(1); // delay in between reads for stability

}
/*

Fade

This example shows how to fade an LED on pin 9 using the analogWrite()

function.

The analogWrite() function uses PWM, so if you want to change the pin you're

using, be sure to use another PWM capable pin. On most Arduino, the PWM pins

are identified with a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.

This example code is in the public domain.

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

*/

int led = 9; // the PWM pin the LED is attached to

int brightness = 0; // how bright the LED is

int fadeAmount = 5; // how many points to fade the LED by

// the setup routine runs once when you press reset:

void setup() {

// declare pin 9 to be an output:

pinMode(led, OUTPUT);

// the loop routine runs over and over again forever:

void loop() {

// set the brightness of pin 9:

analogWrite(led, brightness);

// change the brightness for next time through the loop:

brightness = brightness + fadeAmount;

// reverse the direction of the fading at the ends of the fade:

if (brightness <= 0 || brightness >= 255) {

fadeAmount = -fadeAmount;

// wait for 30 milliseconds to see the dimming effect

delay(30);}
/*

ReadAnalogVoltage

Reads an analog input on pin 0, converts it to voltage, and prints the result to the Serial Monitor.

Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).

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.

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

*/

// the setup routine runs once when you press reset:

void setup() {

// initialize serial communication at 9600 bits per second:

Serial.begin(9600);

// the loop routine runs over and over again forever:

void loop() {

// read the input on analog pin 0:

int sensorValue = analogRead(A0);

// Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V):

float voltage = sensorValue * (5.0 / 1023.0);

// print out the value you read:

Serial.println(voltage);

}
/* Sweep

by BARRAGAN <http://barraganstudio.com>

This example code is in the public domain.

modified 8 Nov 2013

by Scott Fitzgerald

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

*/

#include <Servo.h>

Servo myservo; // create servo object to control a servo

// twelve servo objects can be created on most boards

int pos = 0; // variable to store the servo position

void setup() {

myservo.attach(9); // attaches the servo on pin 9 to the servo object

void loop() {

for (pos = 0; pos <= 180; pos += 1) { // goes from 0 degrees to 180 degrees

// in steps of 1 degree

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

for (pos = 180; pos >= 0; pos -= 1) { // goes from 180 degrees to 0 degrees

myservo.write(pos); // tell servo to go to position in variable 'pos'

delay(15); // waits 15ms for the servo to reach the position

}
/*

Arduino Starter Kit example

Project 4 - Color Mixing Lamp

This sketch is written to accompany Project 3 in the Arduino Starter Kit

Parts required:

- one RGB LED

- three 10 kilohm resistors

- three 220 ohm resistors

- three photoresistors

- red green and blue colored gels

created 13 Sep 2012

modified 14 Nov 2012

by Scott Fitzgerald

Thanks to Federico Vanzati for improvements

http://www.arduino.cc/starterKit

This example code is part of the public domain.

*/

const int greenLEDPin = 9; // LED connected to digital pin 9

const int redLEDPin = 10; // LED connected to digital pin 10

const int blueLEDPin = 11; // LED connected to digital pin 11

const int redSensorPin = A0; // pin with the photoresistor with the red gel

const int greenSensorPin = A1; // pin with the photoresistor with the green gel

const int blueSensorPin = A2; // pin with the photoresistor with the blue gel
int redValue = 0; // value to write to the red LED

int greenValue = 0; // value to write to the green LED

int blueValue = 0; // value to write to the blue LED

int redSensorValue = 0; // variable to hold the value from the red sensor

int greenSensorValue = 0; // variable to hold the value from the green sensor

int blueSensorValue = 0; // variable to hold the value from the blue sensor

void setup() {

// initialize serial communications at 9600 bps:

Serial.begin(9600);

// set the digital pins as outputs

pinMode(greenLEDPin, OUTPUT);

pinMode(redLEDPin, OUTPUT);

pinMode(blueLEDPin, OUTPUT);

void loop() {

// Read the sensors first:

// read the value from the red-filtered photoresistor:

redSensorValue = analogRead(redSensorPin);

// give the ADC a moment to settle

delay(5);

// read the value from the green-filtered photoresistor:

greenSensorValue = analogRead(greenSensorPin);

// give the ADC a moment to settle

delay(5);

// read the value from the blue-filtered photoresistor:

blueSensorValue = analogRead(blueSensorPin);

// print out the values to the Serial Monitor


Serial.print("raw sensor Values \t red: ");

Serial.print(redSensorValue);

Serial.print("\t green: ");

Serial.print(greenSensorValue);

Serial.print("\t Blue: ");

Serial.println(blueSensorValue);

/*

In order to use the values from the sensor for the LED, you need to do some

math. The ADC provides a 10-bit number, but analogWrite() uses 8 bits.

You'll want to divide your sensor readings by 4 to keep them in range

of the output.

*/

redValue = redSensorValue / 4;

greenValue = greenSensorValue / 4;

blueValue = blueSensorValue / 4;

// print out the mapped values

Serial.print("Mapped sensor Values \t red: ");

Serial.print(redValue);

Serial.print("\t green: ");

Serial.print(greenValue);

Serial.print("\t Blue: ");

Serial.println(blueValue);

/*

Now that you have a usable value, it's time to PWM the LED.

*/

analogWrite(redLEDPin, redValue);

analogWrite(greenLEDPin, greenValue);

analogWrite(blueLEDPin, blueValue);

}
/*

Arduino Starter Kit example

Project 9 - Motorized Pinwheel

This sketch is written to accompany Project 9 in the Arduino Starter Kit

Parts required:

- 10 kilohm resistor

- pushbutton

- motor

- 9V battery

- IRF520 MOSFET

- 1N4007 diode

created 13 Sep 2012

by Scott Fitzgerald

http://www.arduino.cc/starterKit

This example code is part of the public domain.

*/

// named constants for the switch and motor pins

const int switchPin = 2; // the number of the switch pin

const int motorPin = 9; // the number of the motor pin

int switchState = 0; // variable for reading the switch's status


void setup() {

// initialize the motor pin as an output:

pinMode(motorPin, OUTPUT);

// initialize the switch pin as an input:

pinMode(switchPin, INPUT);

void loop() {

// read the state of the switch value:

switchState = digitalRead(switchPin);

// check if the switch is pressed.

if (switchState == HIGH) {

// turn motor on:

digitalWrite(motorPin, HIGH);

} else {

// turn motor off:

digitalWrite(motorPin, LOW);

}
/*

Arduino Starter Kit example

Project 5 - Servo Mood Indicator

This sketch is written to accompany Project 5 in the Arduino Starter Kit

Parts required:

- servo motor

- 10 kilohm potentiometer

- two 100 uF electrolytic capacitors

created 13 Sep 2012

by Scott Fitzgerald

http://www.arduino.cc/starterKit

This example code is part of the public domain.

*/

// include the Servo library

#include <Servo.h>

Servo myServo; // create a servo object

int const potPin = A0; // analog pin used to connect the potentiometer

int potVal; // variable to read the value from the analog pin

int angle; // variable to hold the angle for the servo motor
void setup() {

myServo.attach(9); // attaches the servo on pin 9 to the servo object

Serial.begin(9600); // open a serial connection to your computer

void loop() {

potVal = analogRead(potPin); // read the value of the potentiometer

// print out the value to the Serial Monitor

Serial.print("potVal: ");

Serial.print(potVal);

// scale the numbers from the pot

angle = map(potVal, 0, 1023, 0, 179);

// print out the angle for the servo motor

Serial.print(", angle: ");

Serial.println(angle);

// set the servo position

myServo.write(angle);

// wait for the servo to get there

delay(15);

}
/* Motor Board IR Array Test

This example of the Arduno robot's motor board returns the

values read fron the 5 infrared sendors on the bottom of

the robot.

*/

// include the motor board header

#include <ArduinoRobotMotorBoard.h>

String bar; // string for storing the informaton

void setup() {

// start serial communication

Serial.begin(9600);

// initialize the library

RobotMotor.begin();

void loop() {

bar = String(""); // empty the string

// read the sensors and add them to the string

bar = bar + RobotMotor.IRread(1) + ' ' + RobotMotor.IRread(2) + ' ' + RobotMotor.IRread(3) + ' ' +
RobotMotor.IRread(4) + ' ' + RobotMotor.IRread(5);

// print out the values

Serial.println(bar);

delay(100);

}
/* Motor Core

This code for the Arduino Robot's motor board

is the stock firmware. program the motor board with

this sketch whenever you want to return the motor

board to its default state.

*/

#include <ArduinoRobotMotorBoard.h>

void setup() {

RobotMotor.begin();

void loop() {

RobotMotor.parseCommand();

RobotMotor.process();

S-ar putea să vă placă și