Written by admin
Para presentar el servo en la feria de la ciencia, vamos a poner un montaje para que se pueda controlar desde otro Arduino UNO, funcionando como maestro, y con un potenciómetro conectado, para mover el servo de forma manual remota.
Preparamos la tarjeta Arduino con el montaje del ejemplo Knob, y tomamos ese ejemplo y lo modificamos para que se conecte al servo mediante comunicación I2C y le mande la referencia. El programa nos queda:
// Programa de control de servo 300W con potenciometro // Wire Master Writer // by Nicholas Zambetti <http://www.zambetti.com> // Demonstrates use of the Wire library // Writes data to an I2C/TWI slave device // Refer to the "Wire Slave Receiver" example for use with this #include <Wire.h> byte x = 0; char inData[20]; // Cadena de caracteres char cCad[20]; char ch; byte iiCad=0; // Indice de la cadena byte iiCadb=0; int potpin = 0; // analog pin used to connect the potentiometer int val; // variable to read the value from the analog pin int valanterior; void setup() { Wire.begin(1); // join i2c bus (address optional for master) Wire.onReceive(receiveEvent); Serial.begin(9600); } void loop() { val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023) val = map(val, 0, 1023, 0, 120); // scale it to use it with the servo (value between 0 and 180) if (val!=valanterior) { sprintf(inData,"M%d",val); // Envia la cadena Serial.println(inData); EnviaCad(4,inData); iiCad=0; valanterior=val; } delay(500); } void receiveEvent(int howMany) { iiCadb=0; while (0<Wire.available()) { ch=Wire.read(); cCad[iiCadb]=ch; iiCadb++; } cCad[iiCadb]=0; } void EnviaCad(int Dir,char *Mensaje) { Wire.beginTransmission(Dir); Wire.write(Mensaje); Wire.endTransmission(); }
Tras unos pequeños ajustes, el sistema funciona bien y dejamos listo el servo para su exposición.
Deja una respuesta