1

I have a python program that calculates an integer based on some parameters. I am not including the full code here because because it has a lot of deep learning stuff , but the relevant block is:

#calculating time
def gst(no_signals,numbers): #car bus motorcycle truck
    time= [4,2,3.5,3.5]
    x=0
    for i in range(0,4):
        x+=numbers[i]*time[i]
    gst= int (x/(no_signals +1))
    print(gst)
    return(gst)

Now I want to communicate this gst which I have calculated to an Arduino program that is supposed to control the delays for the leds by an amount gst seconds.

The Arduino circuit and code currently look like:

enter image description here

(note that this picture may show some resistors to be unconnected to the breadboard properly. I have fixed that)

code

(the working of the code is explained in detail in this question:LED not lighting up despite digitalRead displaying 1)

int gst=20; //green signal time initialised to 20
int ns=4;  //no.of signals
int sn=1; //current signal number



//pin numbers for LEDS: {{RYG for signal1},{RYG forr signal2}..}
int signal[4][3]={{2,3,4},{5,6,7},{8,9,10},{11,12,13}};

//indexes for the signal array
int red=0;
int yellow=1;
int green=2;

//pin number of "colour" of signal "i" is given by signal[i][colour]
//example: red led of signal 3: signal[2][0]



void setup()
{
  for(int i=2;i<=13;i++){
    pinMode(i,OUTPUT);
  }
  Serial.begin(9600);
  randomSeed(analogRead(0));
}
  
void loop()
{
    int next_sn= sn+1; //next signal
    if(next_sn==5)
      next_sn=1;
  
    int next2_sn= next_sn+1; //next2next
    if(next2_sn==5)
      next2_sn=1;
  
    int next3_sn= next2_sn+1; //next2next2next
     if(next3_sn==5)
      next3_sn=1;
   
  
   //current signal green (for gst seconds), rest all red.
  
    digitalWrite(signal[sn-1][green],HIGH);
    digitalWrite(signal[sn-1][red],LOW); //this line will make sense 
                                      //for the 2nd iteration
  
    digitalWrite(signal[next_sn-1][red],HIGH);
    digitalWrite(signal[next2_sn-1][red],HIGH);
    digitalWrite(signal[next3_sn-1][red],HIGH);
    
    //for debugging purposes
   
    Serial.println(digitalRead(signal[sn-1][green]));
  
    delay(gst*1000);
  
    //gst seconds elapsed, now current signal should become yellow
  
    digitalWrite(signal[sn-1][green],LOW);
    digitalWrite(signal[sn-1][yellow],HIGH);
  
    delay(5*1000);
    
    
    //snapshot taken for calculating no. of cars, buses etc
    //for now, random numbers used
    //we will need to integrate this block with YOLO.
  
  
     //calculate gst
  
     gst=random(10,20);
  
  //make current signal red
   
    digitalWrite(signal[sn-1][red],HIGH);
    digitalWrite(signal[sn-1][yellow],LOW);
  
  
  //the next signal is our current signal for the next iteration
  //the green signal should turns on for gst seconds in the next
  //iteration,and the red signal turns off
    sn=next_sn;
     
  }

Currently, I am using gst=random(10,20);for updating gst. I want to eventually write something like gst=<gst communicated from python>; in place of this.

How can I achieve this?

There's also a second problem: The Arduino program and code run on a simulation website (tinkercad). So I am a bit confused with how exactly we can enable communication. Normally, you stick a USB cable to do that but I obviously cant do that since the arduino is on a simulation website.

simulation link: https://www.tinkercad.com/things/4TzYWouocTv

How can I proceed further?

3
  • You can use pyserial in python to write data to the serial line (append a newline character as a termination byte). On the arduino side you do Serial.readUntil('\n') to retrieve messages. Idk how you would feed serial data to the simulation, though. – Sim Son May 9 at 14:12
  • your whole post is pointless because you cannot proceed until you establish a communication link between Python on your computer and the TinkerCAD simulation .... as such, the question is not about the Arduino at all ... it is about the usage of TinkerCAD ... please check the TinkerCAD forums – jsotola May 9 at 18:22
  • see the 'Communication' examples in Arduino IDE Examples menu – Juraj May 9 at 18:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.