Hey, I am new on this forum, and new to the arduino platform, I want to control speed dc motor with L298 and arduino uno rev3. Problem isi dont know how to read a value between 0 and 255(8 bit value) from serial monitor(i task it), and after arduino read this value . need to set the duty cycle on analog write. Basicly the value that i task on serial is the duty cycle of PWM. I have a some kind of code but is does'nt work. Somebody more expert?
int M1 = 11;
int M2 = 12;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(M1, OUTPUT);
pinMode(M2, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (Serial.available())
{
char ch = Serial.read();
if (ch >= '0' && ch <= '256')
{
Servo test code that uses int n = readString.toInt(); to convert a neumeric character String into a number.
// zoomkat 10-22-11 serial servo test
// type servo position 0 to 180 in serial monitor
// or for writeMicroseconds, use a value like 1500
// for IDE 0022 and later
// Powering a servo from the arduino usually *DOES NOT WORK*.
String readString;
#include <Servo.h>
Servo myservo; // create servo object to control a servo
void setup() {
Serial.begin(9600);
myservo.writeMicroseconds(1500); //set initial servo position if desired
myservo.attach(7, 500, 2500); //the pin for the servo control, and range if desired
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
// auto select appropriate value, copied from someone elses code.
if(n >= 500)
{
Serial.print("writing Microseconds: ");
Serial.println(n);
myservo.writeMicroseconds(n);
}
else
{
Serial.print("writing Angle: ");
Serial.println(n);
myservo.write(n);
}
readString=""; //empty for next input
}
}
There is a potential bug in that reading the digits can be broken in 2 or more parts e.g. if time between digits is longer than the delay used.
It would be better to wait for a non-digit before parsing the int
while (Serial.available())
{
char c = Serial.read();
if ('0'<=c && c <= '9') readString += c;
else break;
delay(2);
}
There is a potential bug in that reading the digits can be broken in 2 or more parts e.g. if time between digits is longer than the delay used.It would be better to wait for a non-digit before parsing the int
That would assume a non digit is sent, such as a delimiter, along with the data. There is no appreciable delay in sending characters as a string from the serial monitor, so the delay in the code accommodates this. The code is just intended to be a very simple example using the serial monitor without changing the settings of the serial monitor, or sending anything beyond just the data desired.