Hi,
I have modified my code to get a reliable serial communication and to use a standalone ATMEGA328P-PU microcontroller.
The issue now is the system works perfectly on Arduino IDE serial monitor, however when I tried to invoke the system via python it does not respond.
Can you please help me to overcome this issue.
Arduino Code:
#include <LiquidCrystal.h>
LiquidCrystal lcd(7, 6, 5, 4, 3, 2);
const uint8_t max_valves = 7;
uint8_t master_valve_id = 0; //master valve is ledPin[10] which is pin 12 in Arduino
uint8_t myInts[max_valves]; // to store the input from serial
unsigned long
time_now = 0,
time_past = 0;
unsigned long valves_delays[ max_valves ];
uint8_t current_valve_id = 0; //first valve to turn on
byte valves[max_valves] = {
B00000011,
B00000101,
B00001001,
B00010001,
B00100001,
B01000001,
B10000001};
char* areas[max_valves] = {
"Back Grdn Beds",
"Alfresco",
"Backyard 1",
"Backyard 2",
"Frontyard 1",
"Frontyard 2",
"Frnt Grdn Beds",
};
//Pin connected to Data in (DS) of 74HC595
const int dataPin = 10;
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 11;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
const int led = 13;
void setup()
{
Serial.begin(9600);
// Serial.setTimeout(1);
lcd.begin (20,4);
lcd.clear();
lcd.setCursor(0,1);
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(led, OUTPUT);
DisableValve(current_valve_id); // turn the LED off by making the voltage LOW
}
void loop()
{
if(Serial.available()>0){
char letter = Serial.read();
if (letter == 115){
time_past = millis();
Serial.println(letter);
DisableValve(current_valve_id); // turn the LED off by making the voltage LOW
time_array();
}
}
test_time();
}
void test_time(){
for (uint8_t j = 0; j< max_valves; j++){
if (valves_delays[j] > 0)
run_valves();
}
}
void time_array(){
for(uint8_t i = 0 ; i < max_valves; i++) {
myInts[i] = Serial.parseInt();
Serial.println(myInts[i]);
}
Serial.println("done");
time_array_new();
}
void time_array_new(){
uint8_t z,y = 0 ;
for( z = 0 ; z < max_valves; z++) {
y = y + myInts[z];
}
if (y > 0)
{
convert_ul();
enable_mastervalve();
}
else
{
DisableValve(current_valve_id); // turn the LED off by making the voltage LOW
}
}
void convert_ul(){
for(uint8_t k = 0 ; k < max_valves; k++) {
valves_delays[k] = 60UL * 1000 * myInts[k];
}
}
void enable_mastervalve(){
current_valve_id = 0;
for (uint8_t l=0; l<max_valves; l++){
if (valves_delays[current_valve_id] > 0){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("LED ON");
EnableValve(current_valve_id );
break;
}
}
}
void run_valves(){
if ( current_valve_id < max_valves)
{
time_now = millis();
if (time_now - time_past > 1000){
Serial.println(time_now - time_past);
lcd.setCursor(0,3);
int rem_time = myInts[current_valve_id]-(time_now - time_past)/1000/60;
if( rem_time < 10){
lcd.print("Mins Left: ");
lcd.print(rem_time);
}
else if(rem_time < 100){
lcd.print("Mins Left: ");
lcd.print(rem_time);
}
else {
lcd.print("Mins Left: ");
lcd.print(rem_time);
}
}
if ( time_now - time_past > valves_delays[ current_valve_id ] )
{
time_past = time_now;
DisableValve(current_valve_id );
current_valve_id = current_valve_id + 1;
if ( current_valve_id < max_valves ){
if (valves_delays[current_valve_id] > 0)
{
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("LED ON");
EnableValve(current_valve_id );
}
else {
jump_valve_id();
if ( current_valve_id < max_valves){
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
Serial.println("LED ON");
EnableValve(current_valve_id );
}
else
DisableValve(current_valve_id );
}
}
else
DisableValve(current_valve_id ); // turn the LED off by making the voltage LOW
}
}
}
void jump_valve_id(){
uint8_t l =0;
uint8_t m = max_valves - current_valve_id;
for (uint8_t l=0; l<m; l++){
if (valves_delays[current_valve_id] > 0)
break;
else
current_valve_id = current_valve_id + 1;
}
}
void DisableValve(uint8_t valve_id )
{
registerWrite(B00000000);
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
Serial.println("LED Off");
valves_delays[valve_id] = 0;
lcd.clear();
lcd.setCursor(3,1);
lcd.print("Sprinklers Off");
}
void EnableValve(uint8_t valve_id )
{
registerWrite(valves[valve_id]);
}
void registerWrite(byte valve_id2) {
byte bitToSet = 0;
digitalWrite(latchPin, LOW);
bitToSet = valve_id2;
shiftOut(dataPin, clockPin, MSBFIRST,bitToSet);
digitalWrite(latchPin, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Sprinklers On");
lcd.setCursor(0,1);
lcd.print("Area: ");
lcd.print(areas[current_valve_id]);
lcd.setCursor(0,2);
lcd.print("Run Time: ");
lcd.print(myInts[current_valve_id]);
lcd.print("Mins");
}
Python Code:
#!/usr/bin/env python
import serial, time
text = "s0,0,1,0,0,0,1"
ser = serial.Serial(port='/dev/ttyUSB0', baudrate=9600, timeout=5)
#ser.setDTR(level=False)
byte = ser.read(size=1)
#print byte,
#while byte =="1":
# print text
#ser.setDTR(0)
#time.sleep(0.1)
#ser.setDTR(1)
#time.sleep(5)
#text2 = ser.read()
print byte
ser.write(text)
print text
# break