How to get Serial Data from Arduino when start signal from python is given?

I want the arduino loop to start when I give a starting signal from python ("y") to the arduino.

The serial data from the arduino should then be send back to python and saved in a csv file.

The code compiles with no errors but it doesn't give me any data. I'd be greatful for some help!

Here is a snippet from my arduino code:

void loop() {
  while(Serial.available() > 0) {
    //InBytes = Serial.readStringUntil('n');
    Temp1 = analogRead(tempsensor1);

    if (InBytes == "y") {
      Serial.write("experiment starts");

      switch(experiment) {
        case 0:
          JoystickValue_X_Y = manual_function();
          Temp1 = analogRead(tempsensor1);  
          break;

        case 11:
          currentTime = millis();    
          if (Temp1 < 23) {
          JoystickValue_X = case_function(1023, 0);
          JoystickValue_Y = case_function(1023, 0);
          Temp1 = analogRead(tempsensor1);  
          }
          else {
          exit(0); 
          }
          break;

my python code goes as follows:

fileName = "temp_force_serialdata.csv"
ser = serial.Serial('COM6', 9600) #arduino port
file = open(fileName, "a")
#time.sleep(2)


i = input("start experiment(y/n):")


if __name__ == 'main':

    while i == "y":
        running = True

    try:
        while (True):
             #get Serial Data (joystick position and tempsensor)
            getData = ser.readline()
            dataString = get.Data.decode('utf-8')
            data = dataString.rstrip()

            dateTimeObj = datetime.now()
            timestampStr = dateTimeObj.strftime("%d-%b-%Y (%H:%M:%S)")

            sensor_data = []
            sensor_data= data.split(";")
            sensor_data.append(timestampStr)

            readings = data.split(";")
            sensor_data.append(readings)

            entry = timestampStr + ',' + '.'.join(readings) + '\n'
            print(entry)

            with open(fileName, 'a', encoding='UTF8') as f:
                f.writer(entry)


            file.close()
            pass

    except KeyboardInterrupt:
        print('Done.')

ser.close()

If someone can help me, I'd be very grateful!!

Please post the full sketch

It's kinda long, but here it is:

#include <Keyboard.h>

//Define variables
const int Led_Green = 9;  
const int Led_Yellow = 10;  
const int Gate1 = 5;
const int Gate2 = 6;
int JoystickValue_X_Y;
const int Joystick_X = A0; 
const int Joystick_Y = A1;
const int pressbutton = 2;
int JoystickValue_X = 0; 
int JoystickValue_Y = 0;
int pressbuttonValue = 0;
const int middle_X = 512;
const int middle_Y = 512;

unsigned long startTime;
unsigned long currentTime;

int tempsensor1 = A3;
int tempsensor2 = A4;
int tempsensor3 = A2;

int Temp1;
int Temp2;
int Temp3;

String InBytes;


//Define experiment (0 = manual, 1 = vollgas, 2 = , 3 = )
int experiment = 0; 

//Define functions--------------------------------------------------------------------
int manual_function() {
      JoystickValue_X = analogRead (Joystick_X);                        // [0, 1023]
      JoystickValue_X = constrain(JoystickValue_X, middle_X, 1023);     // [512, 1023]
      JoystickValue_X = map(JoystickValue_X, middle_X, 1023, 0, 1023);   // [0, 100]

 
      JoystickValue_Y = analogRead (Joystick_Y);                        // [0, 1023]
      JoystickValue_Y = constrain(JoystickValue_Y, middle_Y, 1023);     // [512, 1023]
      JoystickValue_Y = map(JoystickValue_Y, middle_Y, 1023, 0, 1023);   // [0, 100]

 
      pressbuttonValue = digitalRead(pressbutton);

      return JoystickValue_X;
      return JoystickValue_Y;
      return pressbutton;
}


int case_function( int JoystickInput_X, int JoystickInput_Y) {
  if (currentTime < 45000) {
    JoystickValue_X = JoystickInput_X;
    JoystickValue_Y = JoystickInput_Y;
  }
  else {
    JoystickValue_X = 0;
    JoystickValue_Y = 0;
  }
  return JoystickValue_X;
  return JoystickValue_Y;
}
//---------------------------------------------------------------------------------


void setup() {
  startTime = millis(); //internal timer
  Keyboard.begin();

  pinMode(Joystick_X, INPUT); // X-axis
  pinMode(Joystick_Y, INPUT); // Y-axis
  pinMode(pressbutton,INPUT); // press button
  pinMode(Led_Green,OUTPUT);
  pinMode(Led_Yellow, OUTPUT);
  digitalWrite(pressbutton, HIGH);
  
  Serial.begin(9600); 
}

void loop() {
  while(Serial.available() > 0) {
    //InBytes = Serial.readStringUntil('n');
    Temp1 = analogRead(tempsensor1);
    Temp2 = analogRead(tempsensor2);
    Temp3 = analogRead(tempsensor3);

    if (InBytes == "y") {
      Serial.write("experiment starts");

      switch(experiment) {
        case 0:
          JoystickValue_X_Y = manual_function();
          Temp1 = analogRead(tempsensor1);  
          Temp2 = analogRead(tempsensor2); 
          Temp3 = analogRead(tempsensor3); 
          break;

        case 11:
          currentTime = millis();    
          if (Temp1 < 23) {
          JoystickValue_X = case_function(1023, 0);
          JoystickValue_Y = case_function(1023, 0);
          Temp1 = analogRead(tempsensor1);  
          }
          else {
          exit(0); 
          }
          break;

        case 12:
          currentTime = millis();
          if(Temp2 < 23) {
            JoystickValue_X = case_function(0, 1023);
            JoystickValue_Y = case_function(1023, 0);
            Temp2 = analogRead(tempsensor2);
          }

        case 13:
            currentTime = millis();
            if (Temp1 < 23 && Temp2 < 23) {
            JoystickValue_X = case_function(1023, 1023);
            JoystickValue_Y = case_function(1023, 1023);
            Temp2 = analogRead(tempsensor2);
            Temp1 = analogRead(tempsensor1);  
            }
            else {
            exit(0); 
            }  
            break;

            
        case 21:
            currentTime = millis(); 
            if (Temp1 > 36) {
            JoystickValue_X = case_function(1023, 0);
            JoystickValue_Y = case_function(1023, 0);
            Temp1 = analogRead(tempsensor1);      }
            else {
            exit(0); 
            }
            break;

        case 22:
            currentTime = millis();
            if (Temp2 > 36) {
            JoystickValue_X = case_function(0, 1023);
            JoystickValue_Y = case_function(0, 1023);
            Temp2 = analogRead(tempsensor2);;
            }
            else {
            exit(0); 
            }
            break;

       case 23:
            currentTime = millis();
            if (Temp1 > 36 && Temp2 > 36) {
            JoystickValue_X = case_function(1023, 1023);
            JoystickValue_Y = case_function(1023, 1023);
            Temp1 = analogRead(tempsensor1);
            Temp2 = analogRead(tempsensor2);
            }
            else {
            exit(0); 
            }
            break;
        
      }}
    
    else {
      Serial.write("wrong starting signal");
      exit(0);
    }}
  
//  else {
//    Serial.write("no starting signal");
//  }
    
  

  Serial.print(JoystickValue_X);
  Serial.print(";");
  Serial.print(JoystickValue_Y);
  Serial.print(";");
  Serial.print(pressbuttonValue);
  Serial.print(";");
  Serial.print(Temp1);
  Serial.print(";");
  Serial.print(Temp2);
  Serial.print(";");
  Serial.println(Temp3);
  delay(100);

   if (JoystickValue_Y <= 0) {
      analogWrite(Led_Green, 0);
      analogWrite(Gate1, 0);
    }
  
    if (JoystickValue_Y > 0) {
      analogWrite(Led_Green, JoystickValue_Y/4);
      analogWrite(Gate1, JoystickValue_Y/4);
    }
    
  
   if (JoystickValue_X <= 0) {
      analogWrite(Led_Yellow, 0);
      analogWrite(Gate2, 0);
    }
  
   if (JoystickValue_X > 0) {
      analogWrite(Led_Yellow, JoystickValue_X/4);
      analogWrite(Gate2, JoystickValue_X/4);
    }
  
    if(pressbuttonValue = LOW) {
      analogWrite(Led_Green, JoystickValue_Y/4);
      analogWrite(Led_Yellow, JoystickValue_X/4);
      analogWrite(Gate2, JoystickValue_X/4);
      analogWrite(Gate1, JoystickValue_Y/4);
    } 

}
1 Like

What is the signal from Python that is given to the Arduino. Which kind of Arduino is it? Are you using level shifters?

you might want to use if ( InBytes == 'y' )

Im getting an error if I use 'y': invalid conversion from 'char' to 'const char*' [-fpermissive]
if (InBytes == 'y')

I am using an arduino micro and I want to type "y" in python, which should be a starting signal send to the arduino to start the loop.

What are level shifters?

While you are at it post an anotated schematic as you have it wired. Be sure to show all connections including power and ground. Frizzy pictures are normally useless and do not count. We also would like to know what you are using for power supplies?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.