Calificación:
  • 0 voto(s) - 0 Media
  • 1
  • 2
  • 3
  • 4
  • 5
Registar ambos valores
#1
Hola a todos.
Tengo este codigo es una grilla de 3x3 con switches reed. Tengo un magneto en la primera columna a1,a2,a3 (estos estan programados para estar cerrados al comienzo).
Lo que quiero es que al levantar un iman y llevarlo a la otra posicion. se imprima en pantalla de donde lo tome y donde lo puse.

Actualmente solo dice la posicion donde fue cerrado o la posicion donde fue abierto. Dos veces, asi:


square 8 reed switch Close
B 3 B 3
square 8 reed switch Open
B 3 B 3
square 9 reed switch Close
C 3 C 3.

Me gustaria que dijera, por ejemplo b3c3

Gracias a todos

Este es el codigo:


Código:
# lights LED when reed switch closes

import smbus
import time
import math
from Adafruit_LED_Backpack import Matrix8x8
import I2C_LCD_driver

#Display and Keypad setup

# Initialize the LCD using the pins
lcd = I2C_LCD_driver.lcd()

lcd.lcd_clear()

# create some custom characters
lcd.lcd_display_string('Test',1)

#LED setup
# Create display instance on default I2C address (0x70) and bus number.
display = Matrix8x8.Matrix8x8(address=0x70, busnum=1)

# Initialize the display. Must be called once before using the display.
display.begin()
display.clear()
display.write_display()

# MCP23017  setup
# this program scans both registers one device, giving 2 x 8 = 16 inputs, only 9 of these are used.
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
# this program scans both the A and B registers of one MCP23017 port exapander and returns changes
mbrd = [0xB6,0xFF]
chcol =["A","B","C"]  # column labels
i2cadd=0x21 # the I2c Device address of the MCP23017s (A0-A2)
GPIOn = [0x12, 0x13]
IODIRA = 0x00 # APin direction register for first 8 ie 1 = input or 2= output
IODIRB = 0x01 # B Pin direction register
GPIOA  = 0x12 # Register for inputs
GPIOB  = 0x13 # B Register for inputs
GPPUA= 0x0C  # Register for Pull ups A
GPPUB= 0x0D  # Register for Pull ups B

# Set all A 8 GPA pins as  input. ie set them to 1 oXFF = 11111111
bus.write_byte_data(i2cadd,IODIRA,0xFF)
# Set pull up on GPA pins .ie from default of 0 to 11111111
bus.write_byte_data(i2cadd,GPPUA,0xFF)
# Set all B 8 GPB pins as  input. ie set them to 1 oXFF = 11111111
bus.write_byte_data(i2cadd,IODIRB,0xFF)
# Set pull up on GPB pins .ie from default of 0 to 11111111
bus.write_byte_data(i2cadd,GPPUB,0xFF)

print "starting"
# now look for a change

# Loop until user presses CTRL-C
while True:
  # read the 8 registers

  for l in range(2):  #loops round both registers of MCP23017
    a = bus.read_byte_data(i2cadd,GPIOn[l])
    if a != mbrd[l]: # there has been a change
      c = a ^ mbrd[l]  # bitwise operation copies the bit if it is set in one operand but not both.
      dirx = "Close"
      y = math.frexp(c)[1]  # calculates integer part of log base 2, which is binary bit position
      m=y+l*8
      x =int((m-1)/3)+1   # anodes numbers starts 1
      y =  (2+m)%3   # cathodes number start 0
      if a > mbrd[l] : dirx = "Open"  # if the number gets bigger a 0 has changed to a 1
      y = math.frexp(c)[1]  # calculates integer part of log base 2, which is binary bit position
      w=y+l*8
      x =int((w-1)/3)+1   # anodes numbers starts 1
      y =  (2+w)%3   # cathodes number start 0

      if dirx == "Close":
        display.set_pixel(x, y, 1)  # switch on the LED
        lcd.lcd_clear()
        #lcd.message('Hello\nworld!')
        lcd.lcd_display_string('Square: ',2,)
        lcd.lcd_display_string(str(w),2,8)


      if dirx == "Open":
        display.set_pixel(x, y, 0)  # switch off the LED
        lcd.lcd_clear()

      display.write_display()
      print "square", w, " Reed Switch " , dirx    # chcol[(w+2)%3], (int((w-1)/3))+1
      print chcol[(m+2)%3], (int((m-1)/3))+1,chcol[(w+2)%3], (int((w-1)/3))+1

      mbrd[l]=a  # update the current state of the board
      time.sleep(0.1)
Responder


Salto de foro:


Usuarios navegando en este tema: 1 invitado(s)