Arduino
Led Arduino
- Código:
pinMode(13, OUTPUT);
}
void loop() {
digitalWrite(13, HIGH);
delay(2000);
digitalWrite(13, LOW);
delay(2000);
}
Led:
- Código:
const int ledPIN = 9;
void setup() {
Serial.begin(9600);
pinMode(ledPIN , OUTPUT);
}
void loop(){
digitalWrite(ledPIN , HIGH);
delay(1000);
digitalWrite(ledPIN , LOW);
delay(1000);
void setup() {
pinMode(5,INPUT);pinMode(3,OUTPUT);
}
void loop() {
int boton= digitalRead(5);
if(boton==1){
digitalWrite(3,HIGH);
}
else{
digitalWrite(3,LOW);
}
Semáforo:
- Código:
int rojo=2; int amarillo=4; int verde=7;
void setup() {
pinMode(verde,OUTPUT);
pinMode(amarillo,OUTPUT);
pinMode(rojo,OUTPUT);
}
void loop() {
digitalWrite(verde,HIGH);
delay(2000);
digitalWrite(verde,LOW);
delay(500);
digitalWrite(amarillo,HIGH);
delay(2000);
digitalWrite(amarillo,LOW);
delay(500);
digitalWrite(rojo,HIGH);
delay(2000);
digitalWrite(rojo,LOW);
delay(500);
}
Sensor LDR luz:
- Código:
const int ledPin = 13; const int sensorPin = 0; void setup() { Serial.begin(9600); pinMode(ledPin, OUTPUT); } void loop() { int valueAnalog = analogRead(sensorPin); Serial.println(valueAnalog); }
Duelo de reflejos:
- Código:
#define PIN_LED_1 11
#define PIN_BOTON_1 12
#define PIN_LED_2 2
#define PIN_BOTON_2 3
#define PIN_SALIDA 7
byte game_over;
void setup() {
pinMode(PIN_LED_1, OUTPUT);
pinMode(PIN_LED_1, INPUT);
pinMode(PIN_LED_2, OUTPUT);
pinMode(PIN_BOTON_2, INPUT);
pinMode(PIN_SALIDA, OUTPUT);
game_over=0;
digitalWrite(PIN_LED_1,LOW);
digitalWrite(PIN_LED_2,LOW);
digitalWrite(PIN_SALIDA,HIGH);
delay(random(10000));
digitalWrite(PIN_SALIDA,LOW);
}
void loop() {
byte boton_1=digitalRead(PIN_BOTON_1);
byte boton_2=digitalRead(PIN_BOTON_2);
if((boton_1==1)&&(game_over==0)){
digitalWrite(PIN_LED_1,HIGH);
game_over=1;
}
if((boton_2==1)&&(game_over==0)){
digitalWrite(PIN_LED_2,HIGH);
game_over=1;
}
delay(10);
}
#define PIN_BOTON_1 12
#define PIN_LED_2 2
#define PIN_BOTON_2 3
#define PIN_SALIDA 7
byte game_over;
void setup() {
pinMode(PIN_LED_1, OUTPUT);
pinMode(PIN_LED_1, INPUT);
pinMode(PIN_LED_2, OUTPUT);
pinMode(PIN_BOTON_2, INPUT);
pinMode(PIN_SALIDA, OUTPUT);
game_over=0;
digitalWrite(PIN_LED_1,LOW);
digitalWrite(PIN_LED_2,LOW);
digitalWrite(PIN_SALIDA,HIGH);
delay(random(10000));
digitalWrite(PIN_SALIDA,LOW);
}
void loop() {
byte boton_1=digitalRead(PIN_BOTON_1);
byte boton_2=digitalRead(PIN_BOTON_2);
if((boton_1==1)&&(game_over==0)){
digitalWrite(PIN_LED_1,HIGH);
game_over=1;
}
if((boton_2==1)&&(game_over==0)){
digitalWrite(PIN_LED_2,HIGH);
game_over=1;
}
delay(10);
}
Sensor ultrasonidos:
- Código:
const int EchoPin = 5;
const int TriggerPin = 6;
const int LedPin = 13;
void setup() {
Serial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(TriggerPin, OUTPUT);
pinMode(EchoPin, INPUT);
}
void loop() {
int cm = ping(TriggerPin, EchoPin);
Serial.print("Distancia: ");
Serial.println(cm);
delay(1000);
}
int ping(int TriggerPin, int EchoPin) {
long duration, distanceCm;
digitalWrite(TriggerPin, LOW);
delayMicroseconds(4);
digitalWrite(TriggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(TriggerPin, LOW);
duration = pulseIn(EchoPin, HIGH);
distanceCm = duration * 10 / 292/ 2;
return distanceCm;
}
Coche fantastico:
- Código:
int i=0;
void setup(){
for(i=0;i<7;i++)
{
pinMode(pinLEDs[i],OUTPUT);
}
}
void loop() {
for(i=0;i<7;i++)
{
digitalWrite(pinLEDs[i],HIGH);
delay(50);
digitalWrite(pinLEDs[i],LOW);
delay(50); }
for(i=6;i>=0;i—)
{
digitalWrite(pinLEDs[i],HIGH);
delay(50);
digitalWrite(pinLEDs[i],LOW);
delay(50);}}
Cuenta atrás:
Código:
#define Pin_a 3
#define Pin_b 4
#define Pin_c 7
#define Pin_d 6
#define Pin_e 5
#define Pin_f 2
#define Pin_g 1
void setup()
{
pinMode(Pin_a,OUTPUT);
pinMode(Pin_b,OUTPUT);
pinMode(Pin_c,OUTPUT);
pinMode(Pin_d,OUTPUT);
pinMode(Pin_e,OUTPUT);
pinMode(Pin_f,OUTPUT);
pinMode(Pin_g,OUTPUT);
}
void loop()
{
nueve();
delay(1000);
ocho();
delay(1000);
siete();
delay(1000);
seis();
delay(1000);
cinco();
delay(1000);
cuatro();
delay(1000);
tres();
delay(1000);
dos();
delay(1000);
uno();
delay(1000);
cero();
delay(1000);
}
void cero()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,LOW);
digitalWrite(Pin_e,LOW);
digitalWrite(Pin_f,LOW);
digitalWrite(Pin_g,HIGH);
}
void uno ()
{
digitalWrite(Pin_a,HIGH);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,HIGH);
digitalWrite(Pin_e,HIGH);
digitalWrite(Pin_f,HIGH);
digitalWrite(Pin_g,HIGH);
}
void dos()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,HIGH);
digitalWrite(Pin_d,LOW);
digitalWrite(Pin_e,LOW);
digitalWrite(Pin_f,HIGH);
digitalWrite(Pin_g,LOW);
}
void tres()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,LOW);
digitalWrite(Pin_e,HIGH);
digitalWrite(Pin_f,HIGH);
digitalWrite(Pin_g,LOW);
}
void cuatro()
{
digitalWrite(Pin_a,HIGH);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,HIGH);
digitalWrite(Pin_e,HIGH);
digitalWrite(Pin_f,LOW);
digitalWrite(Pin_g,LOW);
}
void cinco()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,HIGH);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,LOW);
digitalWrite(Pin_e,HIGH);
digitalWrite(Pin_f,LOW);
digitalWrite(Pin_g,LOW);
}
void seis()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,HIGH);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,LOW);
digitalWrite(Pin_e,LOW);
digitalWrite(Pin_f,LOW);
digitalWrite(Pin_g,LOW);
}
void siete()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,HIGH);
digitalWrite(Pin_e,HIGH);
digitalWrite(Pin_f,HIGH);
digitalWrite(Pin_g,HIGH);
}
void ocho()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,LOW);
digitalWrite(Pin_e,LOW);
digitalWrite(Pin_f,LOW);
digitalWrite(Pin_g,LOW);
}
void nueve()
{
digitalWrite(Pin_a,LOW);
digitalWrite(Pin_b,LOW);
digitalWrite(Pin_c,LOW);
digitalWrite(Pin_d,HIGH);
digitalWrite(Pin_e,HIGH);
digitalWrite(Pin_f,LOW);
digitalWrite(Pin_g,LOW);
}Servo:
Código:
#include <Servo.h>
Servo myservo; // crea el objeto servo
int pos = 0; // posicion del servo
void setup() {
myservo.attach(9); // vincula el servo al pin digital 9
}
void loop() {
//varia la posicion de 0 a 180, con esperas de 15ms
for (pos = 0; pos <= 180; pos += 1)
{
myservo.write(pos);
delay(15);
}
//varia la posicion de 0 a 180, con esperas de 15ms
for (pos = 180; pos >= 0; pos -= 1)
{
myservo.write(pos);
delay(15);
}
}
Comentarios
Publicar un comentario