0

I am trying to read from 4 inputs to get an integer value but when I test my code the actual value does not match the expected

My Code:

import RPi.GPIO as GPIO
import os, time
i=0
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(22,GPIO.IN)
GPIO.setup(23,GPIO.IN)
GPIO.setup(4,GPIO.IN)
GPIO.setup(17,GPIO.IN)
GPIO.setup(27,GPIO.OUT)
a = [0,0,0,0,0]
while True:
 n1=GPIO.input(22)
 n2=GPIO.input(23)
 n3=GPIO.input(4)
 n4=GPIO.input(17)
 if(n1==0):
  if(n2==0):
   if(n3==0):
    if(n4==0):#0000
     a[i]=0
 if(n1==0):
  if(n2==0):
   if(n3==0):
    if(n4==1):#0001
     a[i]=1
 if(n1==0):
  if(n2==0):
   if(n3==1):
    if(n4==0):#0010
     a[i]=2
 if(n1==0):
  if(n2==0):
   if(n3==1):
    if(n4==1):#0011
     a[i]=3
 if(n1==0):
  if(n2==1):
   if(n3==0):
    if(n4==0):#0100
     a[i]=4
 if(n1==0):
  if(n2==1):
   if(n3==0):
    if(n4==0):#0101
     a[i]=5    
 if(n1==0):
  if(n2==1):
   if(n3==1):
    if(n4==0):#0110
     a[i]=6
 if(n1==0):
  if(n2==1):
   if(n3==1):
    if(n4==1):#0111
     a[i]=7
 i=i+1
 print a
 if(i==5):
  break

Output is always a=[0,0,0,0,0] so, I want to know what is the problem in my code?

3
  • We are rather stymied as you haven't bothered to tell us the input values, what results you expect, and the actual results. You could replace all the ifs with one statement: a[i] = (n1<<3) | (n2<<2) | (n3 << 1) | (n4 <<0).
    – joan
    Jun 25, 2015 at 7:35
  • @joan in all cases lets say 5 4 3 2 1 (each number in binary format) a output is [0,0,0,0,0]
    – Marc Foi
    Jun 25, 2015 at 8:01
  • I would highly suggest refactoring that. There are so many ways in which that could more easily be written. For example, why are you repeating if n1==0: over and over again?
    – Jacobm001
    Jun 25, 2015 at 15:54

1 Answer 1

2

I'm not sure what is wrong with your code (apart from the test for #0101 is wrong).

I've converted it to pigpio as it's much easier to test.

#!/usr/bin/env python

import pigpio

i=0

a = [0,0,0,0,0]

pi = pigpio.pi()

while True:
   n1=pi.read(22)
   n2=pi.read(23)
   n3=pi.read(4)
   n4=pi.read(17)

   a[i] = (n1<<3) | (n2<<2) | (n3 << 1) | (n4 <<0)
   i=i+1
   print i,a
   if(i==5):
      break

pi.stop()

.

$ pigs w 22 0 w 23 0 w 4 0 w 17 0
$ /code/forum/MarcFoi_2.py  
1 [0L, 0, 0, 0, 0]
2 [0L, 0L, 0, 0, 0]
3 [0L, 0L, 0L, 0, 0]
4 [0L, 0L, 0L, 0L, 0]
5 [0L, 0L, 0L, 0L, 0L]
$ pigs w 22 0 w 23 0 w 4 0 w 17 1
$ /code/forum/MarcFoi_2.py  
1 [1L, 0, 0, 0, 0]
2 [1L, 1L, 0, 0, 0]
3 [1L, 1L, 1L, 0, 0]
4 [1L, 1L, 1L, 1L, 0]
5 [1L, 1L, 1L, 1L, 1L]
$ pigs w 22 0 w 23 1 w 4 0 w 17 1
$ /code/forum/MarcFoi_2.py  
1 [5L, 0, 0, 0, 0]
2 [5L, 5L, 0, 0, 0]
3 [5L, 5L, 5L, 0, 0]
4 [5L, 5L, 5L, 5L, 0]
5 [5L, 5L, 5L, 5L, 5L]
$ pigs w 22 1 w 23 1 w 4 0 w 17 1
$ /code/forum/MarcFoi_2.py  
1 [13L, 0, 0, 0, 0]
2 [13L, 13L, 0, 0, 0]
3 [13L, 13L, 13L, 0, 0]
4 [13L, 13L, 13L, 13L, 0]
5 [13L, 13L, 13L, 13L, 13L]
$ pigs w 22 1 w 23 1 w 4 1 w 17 1
$ /code/forum/MarcFoi_2.py  
1 [15L, 0, 0, 0, 0]
2 [15L, 15L, 0, 0, 0]
3 [15L, 15L, 15L, 0, 0]
4 [15L, 15L, 15L, 15L, 0]
5 [15L, 15L, 15L, 15L, 15L]
$ pigs w 22 1 w 23 0 w 4 0 w 17 1
$ /code/forum/MarcFoi_2.py  
1 [9L, 0, 0, 0, 0]
2 [9L, 9L, 0, 0, 0]
3 [9L, 9L, 9L, 0, 0]
4 [9L, 9L, 9L, 9L, 0]
5 [9L, 9L, 9L, 9L, 9L]
$ 
8
  • Thanks @joan , now part of the problem has been solved (always output [0,0,0,0,0]) but there is a problem it should take new input in each loop so the array value should be different for different input,why it does not take different input in each loop .
    – Marc Foi
    Jun 25, 2015 at 8:56
  • 1
    It takes the input available at the start of the loop. I'd have to type very fast to enter new values faster than a Python loop. As a test you could start PWM on the gpios to vary the values rapidly, e.g. pigs p 22 128 p 23 150 p 4 170 p 17 20. What facility have you got to change the gpio values rapidly?
    – joan
    Jun 25, 2015 at 9:20
  • i take the actual values from another device so i can't insure that all values would change faster than the python loop, how to make the loop wait for the changes ?
    – Marc Foi
    Jun 25, 2015 at 9:29
  • I think you should clarify what you are trying to achieve. The question is now unrelated to your original question.
    – joan
    Jun 25, 2015 at 9:34
  • 1
    @dastaan pigs is a command line utility which talks to the pigpio daemon by its socket interface (pigs is part of the pigpio archive). The Python module talks to the daemon via the socket interface as well. You can do pretty much anything you want to do with the gpios by using pigs from the command line. Using pigs above would have changed the gpios from inputs to outputs but that is irrelevant to the script being tested, as when you read from a gpio set as an output you still read the current level.
    – joan
    Jun 25, 2015 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.