PROGRAMACIÓN DE SISTEMAS EMBEBIDOS CON ARDUINO
domingo, 1 de abril de 2012
Laboratorio 7: 4 pulsadores controlando 1 triangulo en Processing
4 PULSADORES CONTROLANDO 1 TRIANGULO EN PROCESSING:
El laboratorio número 7 consiste en mover un triangulo en processing con cuatro pulsadores que controlan cada punto cardinal.
Arduino
// constants won't change. They're used here to // set pin numbers: const int buttonPin2 = 2; // the number of the pushbutton pin const int buttonPin3 = 3; // the number of the pushbutton pin const int buttonPin4 = 4; // the number of the pushbutton pin const int buttonPin5 = 5; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // variables will change: int buttonState2 = 0; // variable for reading the pushbutton status int buttonState3 = 0; // variable for reading the pushbutton status int buttonState4 = 0; // variable for reading the pushbutton status int buttonState5 = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin2, INPUT); pinMode(buttonPin3, INPUT); pinMode(buttonPin4, INPUT); pinMode(buttonPin5, INPUT); Serial.begin(9600); } void loop(){ // read the state of the pushbutton value: buttonState2 = digitalRead(buttonPin2); buttonState3 = digitalRead(buttonPin3); buttonState4 = digitalRead(buttonPin4); buttonState5 = digitalRead(buttonPin5); switch (buttonState2) { case HIGH: digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); Serial.println(2, DEC); delay(1000); break; case LOW: // turn LED off: digitalWrite(ledPin, LOW); break; } switch (buttonState3) { case HIGH: digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); Serial.println(3, DEC); delay(1000); break; case LOW: // turn LED off: digitalWrite(ledPin, LOW); break; } switch (buttonState4) { case HIGH: digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); Serial.println(4, DEC); delay(1000); break; case LOW: // turn LED off: digitalWrite(ledPin, LOW); break; } switch (buttonState5) { case HIGH: digitalWrite(ledPin, HIGH); delay(100); digitalWrite(ledPin, LOW); Serial.println(5, DEC); delay(1000); break; case LOW: // turn LED off: digitalWrite(ledPin, LOW); break; } }
Processing
import processing.serial.*; Serial myPort; // Create object from Serial class int keya = 0; //------------------------------------------------- int x=300; int y=200; int grados=0; void setup() { size(600, 400); background(255, 255, 255); smooth(); // noStroke(); fill(0,0,255); //String portName = Serial.list()[0]; println( Serial.list()); myPort = new Serial(this, "COM4", 9600); } void draw () { if ( myPort.available() > 0) { // If data is available, /* if(myPort.read() >= 50)*/ int temp = myPort.read(); if(temp >= 50) { //println( "DO IT !!!" + keya); keya = temp; println(keya); } /* else keya = 0; */ //println("IF: " + myPort.read()); }else { keya = 0; } background (255, 255, 255); pushMatrix(); translate(x, y); rotate(radians(grados)); triangle(0, 30, -20, -30, 20, -30); popMatrix(); if (keya==50) { y-=160; if (y<40) y=40; } if (keya==52) { y+=160; if (y>400) y=350; } if (keya==51) { grados-=5; if (grados<0) grados=360; x-=200; if (x<10) x=50; } if (keya==53) { grados+=5; if (grados>360) grados=0; x+=200; if (x>600) x=550; } }
No hay comentarios:
Publicar un comentario
Entrada más reciente
Entrada antigua
Inicio
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario