13. Encender y apagar led y mover servomotor con interfaz en python.

Para realizar esta practica tuvimos que hacer uso de Arduino y Pyhton.

Solución en pyhon:
# interface para mover servomotor

#importar librerias
import Tkinter
from Tkinter import *
from time import sleep
from pyfirmata import Arduino, util, SERVO

#configuracion de placa arduino
board = Arduino('COM6')
sleep(5)
board.digital[3].mode = SERVO

#funcion para mover el servomotor
def servo(pocisiones):
    #escritura de angulo en servomotor
    board.digital[3].write(pocisiones)

#encender LED
def led_on():
    board.digital[13].write(1)

#apagar LED
def led_off():
    board.digital[13].write(0)

root = Tk()
root.title("Control de Servomotor")
root.minsize(300,150)

#Barra de angulo
angulo = Scale(root, command=servo, from_=0, to=179, orient=HORIZONTAL, length=300, troughcolor='gray', width=30, cursor='dot', label='Angulo Servo')
angulo.grid(column=2,row=1)

#boton encendido
Bon = Button(root, text="Encender LED", command=led_on)
Bon.grid(column=1, row=3)

#boton apagado
Boff = Button(root, text="Apagar LED", command=led_off)
Boff.grid(column=3, row=3)

root.mainloop()

Solución en arduino:
/*
  Firmata is a generic protocol for communicating with microcontrollers
  from software on a host computer. It is intended to work with
  any host computer software package.

  To download a host software package, please click on the following link
  to open the list of Firmata client libraries in your default browser.

  https://github.com/firmata/arduino#firmata-client-libraries

  Copyright (C) 2006-2008 Hans-Christoph Steiner.  All rights reserved.
  Copyright (C) 2010-2011 Paul Stoffregen.  All rights reserved.
  Copyright (C) 2009 Shigeru Kobayashi.  All rights reserved.
  Copyright (C) 2009-2016 Jeff Hoefs.  All rights reserved.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See file LICENSE.txt for further informations on licensing terms.

  Last updated August 17th, 2017
*/

#include <Servo.h>
#include <Wire.h>
#include <Firmata.h>

#define I2C_WRITE                   B00000000
#define I2C_READ                    B00001000
#define I2C_READ_CONTINUOUSLY       B00010000
#define I2C_STOP_READING            B00011000
#define I2C_READ_WRITE_MODE_MASK    B00011000
#define I2C_10BIT_ADDRESS_MODE_MASK B00100000
#define I2C_END_TX_MASK             B01000000
#define I2C_STOP_TX                 1
#define I2C_RESTART_TX              0
#define I2C_MAX_QUERIES             8
#define I2C_REGISTER_NOT_SPECIFIED  -1

// the minimum interval for sampling analog input
#define MINIMUM_SAMPLING_INTERVAL   1


/*==============================================================================
 * GLOBAL VARIABLES
 *============================================================================*/

#ifdef FIRMATA_SERIAL_FEATURE
SerialFirmata serialFeature;



Comentarios

Entradas populares de este blog

2.12 Instrucciones lógicas.

2.9 Instrucciones aritméticas.

2.10 Manipulación de la pila.