Intro - Arduino & Processing

Conoce más sobre 🔗Arduino
Conoce más sobre 🔗Processing
Conoce más sobre 🔗Bluetooth
Conoce más sobre 🔗AppInventor

void setup() {
  pinMode(13,OUTPUT);  
}
void loop() {
  digitalWrite(13,LOW);
  delay(500);
  digitalWrite(13,HIGH);
  delay(500);
}



Version 1

float tiempoLed = 0.5; // segundos
int led = 13;
long tiempoAnterior;
void setup() {
 pinMode(led, OUTPUT);
 tiempoAnterior = millis();
}
void loop() {
 if (millis() - tiempoAnterior > tiempoLed * 1000) { // multiplicamos por mil para segundos
 tiempoAnterior = millis();
 digitalWrite(led, !digitalRead(led));
 }
}

Versión 2

unsigned long time;
unsigned long aux = 0;
const long inter = 5000;
int Led =  LED_BUILTIN;
int Estado = LOW;
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(Led,OUTPUT);
}
void loop() {
  // put your main code here, to run repeatedly:
   
 time = millis();
 if( time - aux >= inter){
   aux = time;
   if (Estado == LOW){
    Estado = HIGH;
    } else {
      Estado = LOW;
      }
  digitalWrite(Led,Estado);
  Serial.print("Tiempo: ");
  Serial.println(time);
  }
}


Resistencia Externa PULL-DOWN 

 void setup() {
  pinMode(2,INPUT);    
  pinMode(13,OUTPUT);  
}
void loop() {
  if(digitalRead(2)==HIGH){
    digitalWrite(13,HIGH);
  }
  else{
    digitalWrite(13,LOW);
  }
}


Resistencia Interna PULL-UP (Resistencias internas de Arduino) 

void setup() {
  pinMode(2,INPUT_PULLUP);    
  pinMode(13,OUTPUT);  
}
void loop() {
  if(digitalRead(2)==HIGH){
    digitalWrite(13,LOW);
  }
  else{
    digitalWrite(13,HIGH);
  }
}

Resistencia Externa PULL-UP

 void setup() {
  pinMode(2,INPUT);    
  pinMode(13,OUTPUT);  
}
void loop() {
  if(digitalRead(2)==HIGH){
    digitalWrite(13,LOW);
  }
  else{
    digitalWrite(13,HIGH);
  }
}



PullUp en Emulador

void setup() {
 
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(13, OUTPUT);
}
void loop() {
  
  int aux = digitalRead(2);
  
  Serial.println(aux);
  if (aux == HIGH) {
    digitalWrite(13, LOW);
  } else {
    digitalWrite(13, HIGH);
  }
}


Cambio de velocidad

//Puertos
int LED =13;
int Pboton =5; //Aumenta
int Sboton =6; //disminuye
long aux;
unsigned long time ;
long parpadeo = 1000;
void setup(){
  Serial.begin (9600);
  pinMode(LED, OUTPUT);
  pinMode(Pboton, INPUT);
  pinMode(Sboton, INPUT);
  time = millis();
}
void loop (){
 
if(digitalRead(Pboton)==HIGH){
  Serial.print("Valor Inicial Guardado :");
  Serial.println(parpadeo);
  //delay(2000);
  parpadeo = parpadeo + 5000;
  Serial.print("Valor Modificado Aumento :");
  Serial.println(parpadeo);
  //delay(2000);
  if (millis() - time >= parpadeo) { 
    time = millis();
 digitalWrite(LED, !digitalRead(LED));
       Serial.print("Estado del led :");
       Serial.println(digitalRead(LED));
 }
}
if(digitalRead(Sboton)==HIGH){
  Serial.print("Valor Inicial Guardado :");
  Serial.println(parpadeo);
  //delay(2000);
 parpadeo = parpadeo - 5000;
  Serial.print("Valor Modificado reduccion :");
  Serial.println(parpadeo);
 if(parpadeo < 0){
    parpadeo=1000;
  Serial.print("Valor Modificado VeriFicado  Nuevo:");
  Serial.println(parpadeo);
     if (millis() - time > parpadeo) { 
        time = millis();
        digitalWrite(LED, !digitalRead(LED));
       Serial.print("Estado del led :");
       Serial.println(digitalRead(LED));
 }else
  Serial.print("Valor Modificado VeriFicado :");
  Serial.println(parpadeo);
     if (millis() - time > parpadeo) { 
        time = millis();
        digitalWrite(LED, !digitalRead(LED));
       Serial.print("Estado del led :");
       Serial.println(digitalRead(LED));
      }
    }   
  }
  if(digitalRead(Pboton) == LOW && digitalRead(Sboton) == LOW){
  Serial.print("Valor Inicial :");
  Serial.println(parpadeo);
  //delay(2000);
    if (millis() - time >= parpadeo){
     time = millis();
      digitalWrite(LED, !digitalRead(LED));
       Serial.print("Estado del led :");
       Serial.println(digitalRead(LED));
    }
  }
}
  

Parte 1: Arduino

boolean red = false;
boolean green = false;
boolean blue = false;
void setup() {
  pinMode(6, OUTPUT);   //Blue
  pinMode(8, OUTPUT);   //Green
  pinMode(10, OUTPUT);  //Red
  Serial.begin(9600);
}
void loop() {
  if (Serial.available()) {
    char lectura = Serial.read();
    if (lectura == 'r' || lectura == 'R')
      red = !red;
    if (lectura == 'g' || lectura == 'G')
      green = !green;
    if (lectura == 'b' || lectura == 'B')
      blue = !blue;
    digitalWrite(10,red);
    digitalWrite(8,green);
    digitalWrite(6,blue);    
  }
}


Parte 2: Processing

import processing.serial.*;
Serial puerto;
void setup() {
  String COM3 = Serial.list()[0];
  puerto = new Serial(this, COM3, 9600);
}
void draw() {
}
void keyPressed() {
  if (key=='r' || key=='R')
    puerto.write('r');
  if (key=='b' || key=='B')
    puerto.write('b');
  if (key=='g' || key=='g')
    puerto.write('g');
}


Parte 1: Arduino

//Botones
int BotonR = 6;
int BotonG = 8;
int BotonB = 10;
int sent = 0;
void setup() {
  // put your setup code here, to run once:
 pinMode(BotonR, INPUT);
 pinMode(BotonG, INPUT);
 pinMode(BotonB, INPUT);
 Serial.begin(9600);
}
void loop() {
if(digitalRead(BotonR) == HIGH){
  Serial.print('1');
  sent =0;
  }
 else if(digitalRead(BotonG) == HIGH){
  Serial.print('2');
  sent = 0;
  }
 else if(digitalRead(BotonB) == HIGH){
  Serial.print('3');
  sent = 0;
  }else{
    if(sent == 0){
      Serial.print('0');
      sent = 1;
      }
    }
  delay(100);
}

Parte 2: Processing

import processing.serial.*;
Serial myPort;
//String pru;
int val;
//Colores :D
  color ColorFondo = color(0,0,0);
  color rojo = color(211,0,0);
  color verde = color(0,214,46);
  color azul = color (0,74,128);
void setup(){
  size(400,400);
  String portName = Serial.list()[0];
  myPort = new Serial(this, portName, 9600);
}
void draw() { 
  if(0 < myPort.available()){
  val = myPort.read();
  }
background(ColorFondo);
 if(val == '1'){
  background(rojo);
 }else if( val == '2'){
  background(verde);
 }else if( val == '3'){
 background(azul);
 }else{
  background(0);
 }
}

🟢
Binarios            Decimal       ASCCI
01001010            74              J
01001111            79             O
01010100            84             T
01001100            76             L
01000001            65             A
01010110            86             V
01000101            69             E
Palabra Secreta: VOLTAJE
🟢

int BOTON = 2;
int LED_Estima = 3;
int LED_Tiempo = 4;
int segundos;//Para leer el valor de entrada
bool jugando = false;
bool presionado = false;
int tiempo = 0;
void setup() {
  pinMode(LED_Estima, OUTPUT);
  pinMode(LED_Tiempo, OUTPUT);
  pinMode(BOTON, INPUT_PULLUP);
  digitalWrite(LED_Estima, LOW);
  digitalWrite(LED_Tiempo, LOW);
  Serial.begin(9600);
}
void loop() {
  if (Serial.available() > 0 ) { //Si hay disponibilidad en el serial
    int lectura=Serial.read();
    if (!jugando && lectura>=1 && lectura<=9) {
      segundos = lectura;
      titilarLed();
      jugando = true;
      digitalWrite(LED_Estima, HIGH);
      tiempo = millis();
    }
  }
  if (digitalRead(BOTON) == LOW && presionado == false) {
    //El jugador presionó el botón
    if (jugando) { //Si está en modo juego
      int tiempoJugando = millis() - tiempo; //¿Cuántos milisegundos han pasado desde que se inició el juego?
      int deltaPrecision = (segundos * 1000) - tiempoJugando;
      if (deltaPrecision < 0) //Valor Absoluto
        deltaPrecision = deltaPrecision * -1;
      Serial.println(deltaPrecision);
      //Acaba el juego
      jugando = false;
      digitalWrite(LED_Estima, LOW);
    }
    delay(100);
  }
  if (digitalRead(BOTON) == HIGH && presionado == true) {
    presionado = false;
    delay(100);
  }
}
void titilarLed() {
  for (int c = 1; c <= segundos; c++) {
    digitalWrite(LED_Tiempo, HIGH);
    delay(500);
    digitalWrite(LED_Tiempo, LOW);
    delay(500);
  }
}


Reto #9

Código Arduino
  //Leds
int LED_1 = 4;
int LED_2 = 3;
boolean Estado = false;
long Rnumero;
void setup() {
  // put your setup code here, to run once:
 Serial.begin(9600);
 pinMode(LED_1, OUTPUT);
 pinMode(LED_2, OUTPUT);
 //randomSeed(analogRead(A0));
 
}
void loop() {
  // put your main code here, to run repeatedly:
/*if (Serial.available() > 0 ) { //Si hay disponibilidad en el serial
 
}*/
//Serial.print('L');
//delay(1000);
//Generar los numeros aleatorios
Rnumero = random(3,5);
   //Serial.println("El numero es :");
   Serial.println(Rnumero);
if(Rnumero == 4){
   digitalWrite(Rnumero, HIGH);
   Serial.print('L');  
   delay(1000);
   digitalWrite(Rnumero,LOW);
  
}else if(Rnumero == 3){
   digitalWrite(Rnumero, HIGH);
   Serial.print('R');
   delay(1000);
   digitalWrite(Rnumero,LOW);
     
}
}

Código Processing

  int puntos = 0; 
  long  intervalo = 500;
  int aux = 0;
void miPrograma(char c){
  
//println("Entro a mi Programa");
  //println(c);
  char valor = c;
  //Variables para comparar de processing
  char Izq = ' ';
  char Der = ' ';
  //puntaje 
 //Dibujo de Cuadrados
 //Cuadrado derecho
   fill (230,19,0);
   rect(width/2, 0, width/2, height); // Right
 //Cuadrado izquierdo
   fill (251,21,0);
   rect(0, 0, width/2, height); // Left
 //_____________________________________
 if(millis() > aux + intervalo){
    for (int i = 0; i < touches.length; i++)  {
    if ( touches[i].x < width/2) {
      fill (191,16,0);
      rect(0, 0, width/2, height); // Left
      Izq = 'L';
     println(Izq);
    } else {
      fill (128,11,0);
      rect(width/2, 0, width/2, height); // Right
      Der = 'R';
      println(Der);
    }
    if(Izq == valor || Der == valor){
     puntos++;
    }else{
     puntos--;
     if(puntos <= 0){
     puntos= 0;     
     }
    }
    aux = millis();
  }
 }
  tablero ();
    
}
void tablero(){
textSize(100);
fill(255);
text("Puntaje :",250,100);
textSize(100);
fill(255);
text(puntos, 650, 100);
}



Intro - Arduino & Processing
Published: