Hey, I am currently trying to get an serial input into my program and then use that string to get four independent char outputs that I can use to convert them to decimal.
So for better understanding:
Input per Serial -> String gets split into four pieces (input are 4 HEX numbers in one piece) -> convert every single piece into a seperate decimal number
I already got a little bit of code but I cant figure out how to split a string exectly after every two characters or number because I want to split the string after every hex number.
const long baud_rate = 19200;
String input;
void setup() {
Serial.begin(baud_rate);
}
void loop() {
if (Serial.available()) {
input = Serial.readString();
input.trim(); // strip off any leading/trailing space and \r \n
if (input.length() > 0) { // got some input handle it
input.toUpperCase();
Serial.println(input);
}
}
}
You will, of course, be testing the length of the String and rejecting anything too short or too long before processing it. You should also check that any characters received are valid characters for a HEX input
If you are going to do that then you might consider reading the input another way
const long baud_rate = 19200;
int val [4];
char s [90];
void setup() {
Serial.begin(baud_rate);
}
void loop() {
if (Serial.available() > 0) {
char input [50];
int n = Serial.readBytesUntil('\n', input, sizeof(input)-1);
input [n] = '\0';
sscanf (input, "%02x%02x%02x%02x",
& val [0], & val [1], & val [2], & val [3] );
for (int n = 0; n < 4; n++) {
sprintf (s, " %2d: 0x%02x %4d", n, val [n], val [n]);
Serial.println (s);
}
}
}
@gcjr thx for that short example.
I have another question depending on that thread.
I want to control a four pwm pins with a 100Hz pwm signal with those int values. Therefor I found a short information but I am not a software developer. So my question is why is the maximum dutycycle increment only 1-77?
void setup(){
//Initialize Timer2
TCCR2A = 0;
TCCR2B = 0;
TCNT2 = 0;
// Set OC2B for Compare Match (digital pin3)
pinMode(3, OUTPUT);
bitSet(TCCR2A, COM2B1);//clear OC2B on up count compare match
//set OC2B on down count compare match
// Set mode 5-- Phase correct PWM to OCR2A counts up and down
bitSet(TCCR2A, WGM20);
bitSet(TCCR2B, WGM22);
// Set up /1024 prescale
bitSet(TCCR2B, CS20);
bitSet(TCCR2B, CS21);
bitSet(TCCR2B, CS22);
OCR2A = 78; //Sets freq 100Hz
//Timer 2 counts up and down for 156 total counts
//156 x 1024 x.0625 = 9.98ms ~ 100 Hz
//duty cycle is set by OCR2B value
//valid values 1-77
//change it in loop to vary duty cycle
OCR2B = 38;//50% duty cycle
}
So as it looks for me its not possible to get a PWM for an output of an Arduino Uno with a frequency of 100Hz and a range of dutycycle of 0-255?
Because I would loose a lot of steps if I would just calculate the dutycycle out of 0-255.
So after some reading and thinking. Is it possible to change the prescaler so that I get a dutycycle range from 0-255 but I can keep my pwm frequency at 100Hz?
Also is it possible to set up the same for timer 1, so that I can use pin 3, 11 and 10, 9?