Python program doesn't run properly from terminal
Posted: Sun Mar 25, 2018 8:23 am
Hi, this is my first post on here but have been lurking around for a while and learnt a lot so thanks!
My program is taking values from the Arduino analog sensor via USB to the Pi 3 and outputting the bar on the screen. The screen is a 5" hdmi 800x480 and all the drivers are installed and work fine.
I know the function's long but it needs to be quick and responsive for the project.
The program runs fine when I open Python 3 from the menu and use F5 to run.
But the problem comes when I try to run it from the command line (or rc.local or crontab) the serial connection code detects nothing from the Arduino. I can see the TX light flashing on the arduino so I'm guessing some libraries/serial ports are only opened when Python 3 is open? Am I even close?
Here's the arduino code:
int a =0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor_value = analogRead(A0);
int send_pi = map(sensor_value,760,1000,88,0);
Serial.println(send_pi);
delay(10);
}
And for the Pi (Python 3):
#setup screen first
import pygame
pygame.init()
screen = pygame.display.set_mode((800,480), pygame.NOFRAME)
pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
import time
import math
import os
import serial
import re
BLACK=(0,0,0)
WHITE=(255,255,255)
YELLOW = (255,212,0)
ORANGE = (255,158,0)
RED = (255,25,0)
BLUE = (1,102,122)
GREY = (135,135,135)
GREEN=(0,235,65)
#variables to store current and previous sensor values from arduino
FuelPos=1
PrevFuel=0
clock = pygame.time.Clock()
#define font to use
myFont = pygame.font.SysFont("Times New Roman",92)
#setup USB connection with arduino
ser = serial.Serial('/dev/ttyACM0',9600)
#Draw function to draw bar to screen
def DrawFuel(f):
FuelPos = f
if FuelPos > PrevFuel:
if FuelPos <= 31:
for i in range (PrevFuel,FuelPos):
j = i * 5
j = 458 - j
h=j+4
pygame.draw.line(screen,ORANGE,[760,j],[760,h],4)
pygame.draw.line(screen,ORANGE,[760,j],[730,j],5)
pygame.display.flip()
#4
#1
elif 31 < FuelPos <= 48:
if PrevFuel <= 31:
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
elif 48 < FuelPos <= 65:
if PrevFuel <= 31 :
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k = 730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
elif 65 < FuelPos <= 78:
if PrevFuel <= 31:
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
elif 78 < FuelPos <= 88:
if PrevFuel <= 31:
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
#1
elif FuelPos < PrevFuel:
if FuelPos <= 30:
if PrevFuel <= 31:
for i in range(0,FuelPos):
j=i*5
j=458-j
h=j+5
pygame.draw.line(screen,ORANGE,[760,j],[760,h],4)
pygame.draw.line(screen,ORANGE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(FuelPos,PrevFuel):
j=i*5
j=458-j
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[730,j],4)
pygame.display.flip()
#4
#1
elif 31 < PrevFuel <= 89:
for i in range(0,FuelPos):
j=i*5
j=458-j
h=j+2
pygame.draw.line(screen,ORANGE,[760,j],[760,h],4)
pygame.draw.line(screen,ORANGE,[760,j],[730,j],4)
pygame.display.flip()
#4
for i in range(FuelPos,31):
j=i*5
j=458-j
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[730,j],4)
pygame.display.flip()
#4
for i in range(31,PrevFuel):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[k,j],4)
pygame.display.flip()
#4
#3
#1
elif 30 < FuelPos < 88:
for i in range(FuelPos,PrevFuel):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[k,j],4)
pygame.display.flip()
#4
#3
#3
##################### RUN CODE START #########################
DrawFuel(88)
PrevFuel = 88
DrawFuel(0)
PrevFuel = 0
Fr = 1000
a = 0
run = True
while run:
clock.tick(Fr)
for event in pygame.event.get():
if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
run = False
line=str(ser.readline()) #read USB connection from arduino
#extract the numbers from the serial string
if len(line) == 8:
a = int(line[2])
elif len(line) == 9:
b = int(line[2])
b = b * 10
c = int(line[3])
a = b+c
#draw code
pygame.draw.rect(screen,BLACK,[350,150,200,100])
DispNo = myFont.render(str(a),1,WHITE)
screen.blit(DispNo,[350,150])
DrawFuel(a)
PrevFuel = a
pygame.quit()
My program is taking values from the Arduino analog sensor via USB to the Pi 3 and outputting the bar on the screen. The screen is a 5" hdmi 800x480 and all the drivers are installed and work fine.
I know the function's long but it needs to be quick and responsive for the project.
The program runs fine when I open Python 3 from the menu and use F5 to run.
But the problem comes when I try to run it from the command line (or rc.local or crontab) the serial connection code detects nothing from the Arduino. I can see the TX light flashing on the arduino so I'm guessing some libraries/serial ports are only opened when Python 3 is open? Am I even close?
Here's the arduino code:
int a =0;
void setup() {
Serial.begin(9600);
}
void loop() {
int sensor_value = analogRead(A0);
int send_pi = map(sensor_value,760,1000,88,0);
Serial.println(send_pi);
delay(10);
}
And for the Pi (Python 3):
#setup screen first
import pygame
pygame.init()
screen = pygame.display.set_mode((800,480), pygame.NOFRAME)
pygame.mouse.set_cursor((8,8),(0,0),(0,0,0,0,0,0,0,0),(0,0,0,0,0,0,0,0))
import time
import math
import os
import serial
import re
BLACK=(0,0,0)
WHITE=(255,255,255)
YELLOW = (255,212,0)
ORANGE = (255,158,0)
RED = (255,25,0)
BLUE = (1,102,122)
GREY = (135,135,135)
GREEN=(0,235,65)
#variables to store current and previous sensor values from arduino
FuelPos=1
PrevFuel=0
clock = pygame.time.Clock()
#define font to use
myFont = pygame.font.SysFont("Times New Roman",92)
#setup USB connection with arduino
ser = serial.Serial('/dev/ttyACM0',9600)
#Draw function to draw bar to screen
def DrawFuel(f):
FuelPos = f
if FuelPos > PrevFuel:
if FuelPos <= 31:
for i in range (PrevFuel,FuelPos):
j = i * 5
j = 458 - j
h=j+4
pygame.draw.line(screen,ORANGE,[760,j],[760,h],4)
pygame.draw.line(screen,ORANGE,[760,j],[730,j],5)
pygame.display.flip()
#4
#1
elif 31 < FuelPos <= 48:
if PrevFuel <= 31:
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
elif 48 < FuelPos <= 65:
if PrevFuel <= 31 :
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k = 730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
elif 65 < FuelPos <= 78:
if PrevFuel <= 31:
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
elif 78 < FuelPos <= 88:
if PrevFuel <= 31:
for i in range(0,31):
j=i*5
j=458-j
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(31,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#1
else:
for i in range(PrevFuel,FuelPos):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+4
pygame.draw.line(screen,BLUE,[760,j],[760,h],4)
pygame.draw.line(screen,BLUE,[760,j],[k,j],5)
pygame.display.flip()
#4
#3
#3
#1
elif FuelPos < PrevFuel:
if FuelPos <= 30:
if PrevFuel <= 31:
for i in range(0,FuelPos):
j=i*5
j=458-j
h=j+5
pygame.draw.line(screen,ORANGE,[760,j],[760,h],4)
pygame.draw.line(screen,ORANGE,[760,j],[730,j],5)
pygame.display.flip()
#4
for i in range(FuelPos,PrevFuel):
j=i*5
j=458-j
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[730,j],4)
pygame.display.flip()
#4
#1
elif 31 < PrevFuel <= 89:
for i in range(0,FuelPos):
j=i*5
j=458-j
h=j+2
pygame.draw.line(screen,ORANGE,[760,j],[760,h],4)
pygame.draw.line(screen,ORANGE,[760,j],[730,j],4)
pygame.display.flip()
#4
for i in range(FuelPos,31):
j=i*5
j=458-j
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[730,j],4)
pygame.display.flip()
#4
for i in range(31,PrevFuel):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[k,j],4)
pygame.display.flip()
#4
#3
#1
elif 30 < FuelPos < 88:
for i in range(FuelPos,PrevFuel):
j=i*5
j=458-j
k=i-40
k=math.pow(1.12,k)
k=730-k
h=j+2
pygame.draw.line(screen,BLACK,[760,j],[760,h],4)
pygame.draw.line(screen,BLACK,[760,j],[k,j],4)
pygame.display.flip()
#4
#3
#3
##################### RUN CODE START #########################
DrawFuel(88)
PrevFuel = 88
DrawFuel(0)
PrevFuel = 0
Fr = 1000
a = 0
run = True
while run:
clock.tick(Fr)
for event in pygame.event.get():
if event.type == pygame.KEYUP and event.key == pygame.K_ESCAPE:
run = False
line=str(ser.readline()) #read USB connection from arduino
#extract the numbers from the serial string
if len(line) == 8:
a = int(line[2])
elif len(line) == 9:
b = int(line[2])
b = b * 10
c = int(line[3])
a = b+c
#draw code
pygame.draw.rect(screen,BLACK,[350,150,200,100])
DispNo = myFont.render(str(a),1,WHITE)
screen.blit(DispNo,[350,150])
DrawFuel(a)
PrevFuel = a
pygame.quit()