I am trying to monitor the voltage sent to a LED with a python script. To do so I wrote a simple python code that sends the correct amount of bits via the serial port.
Here is my code on the card :
int ledPin = 5;
int freq = 5000;
int ledChannel = 0;
int resolution = 8;
int dutyCycle = 0;
int v2 =0;
void setup(){
Serial.begin(9600);
ledcSetup(ledChannel, freq, resolution);
ledcAttachPin(ledPin, ledChannel);
}
void loop(){
while(Serial.available()){
v2 = Serial.read();
}
Serial.write(v2); // This writes the correct number of bits I sent on the python terminal
ledcWrite(ledChannel, v2);
Serial.println(v2); // This writes 0 every 2s on the arduino terminal
delay(2000);
}
My problem is that it turns the light on only one time when I run the python script, while I want the loop to continue with the value of v2 in memory.
I think my comprehension of the Serial.available() might be wrong.
How can I do to keep the value from Serial.read() once and using it in the next loops ?
Your program is reading a single byte from the Serial port and using it to set the LEDs. I suspect that more than one byte is being sent so the next iteration of loop() gets the next byte and over-writes the value in v2. Be aware that serial data is very slow compared to an Arduino so the while(Serial.available()) can complete before the next byte arrives
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.
The technique in the 3rd example will be the most reliable.
You can send data in a compatible format with code like this (which can also be implemented in Python)
Thanks for your answer, I am checking this tutorial at the moment and I will try different settings.
Actually, I don't want to send char type on the serial port but directly bits between 0 and 255. Is there a way to read just once the value and then keep it in memory in order to send it on the pin ?
lduprat:
Actually, I don't want to send char type on the serial port but directly bits between 0 and 255.
A small modification to my examples will facilitate that. And if you are content with sending values from 0 to 253 you can use 254 and 255 as the start- and end-markers with a loss of less than 1% of the total range.
However sending the values as human readable text will make the debugging very much easier. I only resort to sending binary data when it is essential for performance.
Delta_G:
And that code will never read from Serial again as long as didItAlready remains true.
Is that what you want?
Yes that's the exact idea, however my problem is still there, if I do as you suggest I need to able to change the boolean whenever the user enter a new value. So I wrote this :
And even like this, the loop keeps reading because it puts the boolean 'false' everytime.
I feel like I'm missing something really trivial.
I need to send a data that the loop reads and keep in memory, and to be able to change this data with a new serial communication.
Do you see where is my reasonment wrong ?
Delta_G:
So now you don't want to read it just once. Now you want to be able to change it when there is new serial data. Will you make up your mind please.
Well I said that I wanted to monitor a voltage, so I thought it was quite obvious.
Sorry then, I can be more precise : I want the arduino to read once the instruction given by the user, and keep it as a constant to deliver a constant voltage.
But then, if the user wants to change the voltage, I need my code to be able to replace this constant by the new instruction sent.
For the code, it was a mistake from something I tried just before. The real one is :
lduprat:
I want the arduino to read once the instruction given by the user, and keep it as a constant to deliver a constant voltage.
But then, if the user wants to change the voltage, I need my code to be able to replace this constant by the new instruction sent.
See the examples in Serial Input Basics - simple reliable ways to receive data.
I'm just not allowed to post the code I'm working with on the web.
I appreciate your help, but being an active member does not exempt you from being pleasant.
I love how you just make light of wasting my time with drama posts instead of pedagogic ones.