/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Lines that begin with 2 slashes like this one are COMMENTS.
// The computer ignores the comment lines. Use them to make notes
// for your future self about what the code is doing.
/*
You can also "comment out" multiple lines by enclosing them between the slash-asterisk and asterisk-slash, as shown here
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led1 = 13;
int led2 = 8;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
digitalWrite(led1, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(led2, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
digitalWrite(led1, LOW); // turn the LED off by making the voltage LOW
digitalWrite(led2, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
}
int lightPin = A0; //define a pin for Photoresistor (input)
int ledPin = 13; // the pin for the led (output)
void setup()
{
Serial.begin(9600); //Begin serial communication
pinMode( lightPin, INPUT );
pinMode( ledPin, OUTPUT );
}
void loop()
{
int lightValue = analogRead( lightPin );
Serial.println( lightValue ); //Write the value of the photoresistor to the serial monitor.
// if there is not enough light
if ( lightValue < 100 ){
// turn on the LED
digitalWrite( ledPin, HIGH );
}
else{
// otherwise, turn it off
digitalWrite( ledPin, LOW );
}
delay(10); //short delay for faster response to light
}
/*
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(200);
}
int pwmPin = 12; // assigns pin 12 to variable pwm
int pot = A0; // assigns analog input A0 to variable pot
int c1 = 0; // declares variable c1
int c2 = 0; // declares variable c2
void setup() {
pinMode(pwmPin, OUTPUT);
pinMode(pot, INPUT);
}
void loop() {
c2= analogRead(pot);
c1= 1024-c2; // subtracts c2 from 1000 ans saves the result in c1
digitalWrite(pwmPin, HIGH);
delayMicroseconds(c1);
digitalWrite(pwmPin, LOW);
delayMicroseconds(c2);
}
/*
Into Robotics
*/
#include <Servo.h>
int servoPin = 9;
Servo servo;
int servoAngle = 0; // servo position in degrees
void setup()
{
Serial.begin(9600);
servo.attach(servoPin);
}
void loop()
{
//control the servo's direction and the position of the motor
servo.write(0); // Turn SG90 servo Left to 45 degrees
delay(1000); // Wait 1 second
servo.write(90); // Turn SG90 servo back to 90 degrees (center position)
delay(1000); // Wait 1 second
servo.write(180); // Turn SG90 servo Right to 135 degrees
delay(1000); // Wait 1 second
servo.write(90); // Turn SG90 servo back to 90 degrees (center position)
delay(1000);
//end control the servo's direction and the position of the motor
//control the servo's speed
//if you change the delay value (from example change 50 to 10), the speed of the servo changes
// for(servoAngle = 0; servoAngle < 180; servoAngle++) //move the micro servo from 0 degrees to 180 degrees
// {
// servo.write(servoAngle);
// delay(50);
// }
//
// for(servoAngle = 180; servoAngle > 0; servoAngle--) //now move back the micro servo from 0 degrees to 180 degrees
// {
// servo.write(servoAngle);
// delay(10);
// }
//end control the servo's speed
}
/* FSR testing sketch.
Connect one end of FSR to 5V, the other end to Analog 0.
Then connect one end of a 10K resistor from Analog 0 to ground
Connect LED from pin 11 through a resistor to ground
For more information see www.ladyada.net/learn/sensors/fsr.html */
int fsrAnalogPin = 0; // FSR is connected to analog 0
int LEDpin = 11; // connect Red LED to pin 11 (PWM pin)
int fsrReading; // the analog reading from the FSR resistor divider
int LEDbrightness;
void setup(void) {
Serial.begin(9600); // We'll send debugging information via the Serial monitor
pinMode(LEDpin, OUTPUT);
}
void loop(void) {
fsrReading = analogRead(fsrAnalogPin);
Serial.print("Analog reading = ");
Serial.println(fsrReading);
// we'll need to change the range from the analog reading (0-1023) down to the range
// used by analogWrite (0-255) with map!
LEDbrightness = map(fsrReading, 0, 1023, 0, 255);
// LED gets brighter the harder you press
analogWrite(LEDpin, LEDbrightness);
delay(100);
}
// set up the range finder, HC-SR04
int rangeTriggerPin = 2;
int rangeEchoPin = 3;
long currentDistance; // (cm)
/**
* initialization code, happens once
**/
void setup()
{
// for printing
Serial.begin( 9600 );
// set up the range finder
pinMode(rangeTriggerPin, OUTPUT);
pinMode(rangeEchoPin, INPUT);
}
/**
* loops after setup
**/
void loop()
{
// get current distance
currentDistance = senseCurrentDistance();
// print it out
Serial.println( currentDistance );
// wait a little before next iteration
delay( 500 );
}
/**
* Use the range finder to determine distance to the
* closest object.
**/
long senseCurrentDistance()
{
// The PING is triggered by a HIGH pulse of 2 or more microseconds.
// Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
digitalWrite(rangeTriggerPin, LOW);
delayMicroseconds(2);
digitalWrite(rangeTriggerPin, HIGH);
delayMicroseconds(5);
digitalWrite(rangeTriggerPin, LOW);
// A different pin is used to read the signal from the PING: a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
long duration = pulseIn(rangeEchoPin, HIGH);
return microsecondsToCentimeters( duration );
}
/**
* Determine the distance based on how long it took to echo back.
**/
long microsecondsToCentimeters(long microseconds)
{
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the
// object we take half of the distance travelled.
return microseconds / 29 / 2;
}
int flexSensorPin = A0; //analog pin 0
void setup(){
Serial.begin(9600);
}
void loop(){
int flexSensorReading = analogRead(flexSensorPin);
//Serial.println(flexSensorReading);
//In my tests I was getting a reading on the arduino between 154, and 479.
//Using map(), you can convert that to a larger range like 0-100.
//To find your range uncomment the top serial print
//comment out the one below and then bend your sensor in every direction
// and see what minimum and maximum values you get and then use them below
int flex0to100 = map(flexSensorReading, 154, 479, 0, 100);
Serial.println(flex0to100);
delay(250); //just here to slow down the output for easier reading
}