Hello All, I am fairly new to Arduino and am using it for a class project where my friends and I are developing a robotic arm. We decided to use Python as an interface and were able to send and verify two values with LEDs however we would need to hard code each received character to some place in the Arduino code. It would be preferable to tokenize this information so that we can send numeric variables or other important information. As I said, we did get the code to work when we only sent two values but sending a string has been more difficult. Here is the Python Code:
import serial
from serial import *
import time
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
#from PIL import ImageTk, Image
def GUI():
window = Tk()
window.title("The Egg Bot")
window.geometry("400x250")
#img = ImageTk.PhotoImage(Image.open("Unit_Circle.png"))
#imglabel = Label(window, image=img).place(x=500, y=100)
s = serial.Serial('COM5', 9600)
title = Label(window,
text="Testing Python to Arduino Inputs",
font="Times 32",
anchor = N)
title.pack()
Header = Label(window,
text="Begin by entering the Parameters",
font = "Times 14",
anchor = N)
Header.pack()
def confirmExit():
if messagebox.askokcancel("Quit", "Are you sure you want to quit?"):
window.destroy()
Button(window, text="Quit", command=confirmExit).place(x=5,y=350)
#def Power_Robot_On
#trnon = Button(window, text="Turn On", command=Power_Robot_On)
#trnon.place(x=210, y=298)
#def Power_Robot_Off():
#stp = Button(window, text="Turn Off", command=Power_Robot_Off)
#stp.place(x=200, y=298)
lbl2 = Label(window, text="Position of Egg in X")
lbl2.place(x=5, y=200)
txt_1 = Entry(window,width=10)
txt_1.place(x=120, y=200)
def Button_1():
res1 = "Please Wait... "
lbl2.configure(text = res1)
if str(txt_1['state']) == 'normal':
txt_1.configure(state = 'disable')
btn1 = Button(window, text="OK", command=Button_1)
btn1.place(x=185, y=198)
lbl3 = Label(window, text="Position of Egg in Y")
lbl3.place(x=5, y=250)
txt_2 = Entry(window,width=10)
txt_2.place(x=120, y=250)
def Button_2():
res2 = "Please Wait... "
lbl3.configure(text = res2)
if str(txt_2['state']) == 'normal':
txt_2.configure(state = 'disable')
btn2 = Button(window, text ="OK", command=Button_2)
btn2.place(x=185, y=248)
#lbl4 = Label(window, text="Height")
#lbl4.place(x=5, y=300)
#txt_3 = Entry(window,width=10)
#txt_3.place(x=120, y=300)
#def Button_3():
#res3 = "Please Wait... "
#lbl4.configure(text = res3)
#if str(txt_3['state']) == 'normal':
#txt_3.configure(state = 'disable')
#btn3 = Button(window, text="OK", command=Button_3)
#btn3.place(x=185, y=298)
def Reset_Button():
lbl2.configure(text = "Position of Egg in X")
lbl3.configure(text = "Position of Egg in Y")
#lbl4.configure(text = "Velocity")
txt_1.configure(state = 'normal')
txt_2.configure(state = 'normal')
#txt_3.configure(state = 'normal')
txt_1.delete(0,END)
txt_2.delete(0,END)
#txt_3.delete(0,END)
rst = Button(window, text = "Reset", command=Reset_Button)
rst.place(x=40, y=350)
def Execute_Button():
X = txt_1.get()
Y = txt_2.get()
#Z = txt_3.get()
SX = str(X)
SY = str(Y)
SZ = '*#' + SX + '#' + SY + '#*
They Python code will take the variables, regardless of size, and store them in SX and SY which has special characters that will be dealt with in Arduino.
#include <string.h>
#include <stdio.h>
char i;
char j[10];
int arry[10];
char *strings[10];
String readString;
int d = 0;
int e = 0;
int x;
int y;
int z = 0;
int Length = 0;
bool flag = true;
char *fix = NULL;
byte index = 0;
char arrayt[] = "*#22#33#*$";
///// Flash LED /////
void FlashLED(){
digitalWrite(13,HIGH);
delay(500);
digitalWrite(13, LOW);
delay(500);
}
///// Seperating Strings Test /////
void test(){
Serial.println("Begin Test: ");
Serial.println(arrayt);
while (flag == true){
j[z] = arrayt[z];
readString += j[z];
delay(400);
//arry[z] = (int)(j[z]-'0');
//if (d < z){
//FlashLED();
//d++;
//}
Serial.print("Current Value of z: ");
Serial.println(z);
// Length += 1;
if (j[z] == '
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you.
# s.write(SX.encode())
# s.write(SY.encode())
s.write(SZ.encode())
time.sleep(5)
print(SZ)
Exbtn = Button(window, text = "Execute", command=Execute_Button)
Exbtn.place(x=155, y=348)
window.mainloop()
GUI()
They Python code will take the variables, regardless of size, and store them in SX and SY which has special characters that will be dealt with in Arduino.
§DISCOURSE_HOISTED_CODE_1§
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you. or z == 9){ // 9 is length of string (last char is '
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you.
# s.write(SX.encode())
# s.write(SY.encode())
s.write(SZ.encode())
time.sleep(5)
print(SZ)
Exbtn = Button(window, text = "Execute", command=Execute_Button)
Exbtn.place(x=155, y=348)
window.mainloop()
GUI()
They Python code will take the variables, regardless of size, and store them in SX and SY which has special characters that will be dealt with in Arduino.
§DISCOURSE_HOISTED_CODE_1§
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you. yet it will continue to run regardless, it should stop but doesn't)
flag = false; // End of data, stop recieving
}
else{
z++; // (more data, continue recieving)
}
}
Serial.print("readString: ");
Serial.println(readString);
Serial.print("What's in 'j'?: ");
Serial.println(j);
for (z = 0; z < 10; z++){
Serial.print("Value of z: ");
Serial.print(z);
Serial.print(", Item at location: ");
Serial.println(j[z]);
//Serial.print(" and at arry: ");
//Serial.println(arry[z]);
}
fix = strtok(j, "#$");
while (fix != NULL){
strings[index] = fix;
index++;
fix = strtok(NULL, "#$");
}
for (int n = 0; n < index; n++){
Serial.print("n: ");
Serial.print(n);
Serial.print(", string: ");
Serial.println(strings[n]);
}
x = atoi(strings[1]);
y = atoi(strings[2]);
Serial.print("Val of x: ");
Serial.println(x);
Serial.print("Val of y: ");
Serial.println(y);
}
void read_string(){
if (Serial.available()){
digitalWrite(12, HIGH);
delay(2000);
digitalWrite(12, LOW);
delay(2000);
while (flag == true){
j[z] = Serial.read(); // read first character bit, loops to read next character
delay(400);
//arry[z] = (int)(j[z]-'0'); // turn first character bit into int, then store in a second array, does same for second character
if (j[z] == '
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you.
# s.write(SX.encode())
# s.write(SY.encode())
s.write(SZ.encode())
time.sleep(5)
print(SZ)
Exbtn = Button(window, text = "Execute", command=Execute_Button)
Exbtn.place(x=155, y=348)
window.mainloop()
GUI()
They Python code will take the variables, regardless of size, and store them in SX and SY which has special characters that will be dealt with in Arduino.
§DISCOURSE_HOISTED_CODE_1§
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you. or z == 9){
flag = false;
//Serial.print("Final Value of z: ");
//Serial.println(z);
}
else{
z++;
}
}
//char array[z];
fix = strtok(j, "#$");
while (fix != NULL){
strings[index] = fix;
index++;
fix = strtok(NULL, "#$");
}
x = atoi(strings[1]);
y = atoi(strings[2]);
}
}
void setup() {
// put your setup code here, to run once:
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
//pinMode(10, OUTPUT);
Serial.begin(9600);
}
void loop() {
test(); // cannot have both test and read_string
//read_string(); // to run with Python, comment 'test', uncomment 'read_string'
if (j[0] = '*'){
FlashLED();
delay(2000);
}
if (x == 22){
digitalWrite(12,HIGH); //check if inputs are correct
}
if(y == 33){
digitalWrite(11,HIGH);
}
//Serial.print("Value of j: ");
//Serial.println(j);
//Serial.print("Value of 'arry': ");
//Serial.println(readString);
while(1){}
}
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you.
# s.write(SX.encode())
# s.write(SY.encode())
s.write(SZ.encode())
time.sleep(5)
print(SZ)
Exbtn = Button(window, text = "Execute", command=Execute_Button)
Exbtn.place(x=155, y=348)
window.mainloop()
GUI()
They Python code will take the variables, regardless of size, and store them in SX and SY which has special characters that will be dealt with in Arduino.
§DISCOURSE_HOISTED_CODE_1§
Ultimately we are trying to send over numeric variables and later, probably some sort of flags or boolean values to cause some sort of event to happen. The 'test' section is good example of what I am trying to accomplish, separating everything between the delimiters as a string and then converting the necessary strings into integers to be used for operations. Any advice or direction is greatly appreciated, thank you.