Go Down

Topic: Sending a variable over serial to arduino. (Read 4362 times) previous topic - next topic


Im suddenly getting confused with all this byte, char, string, null  =(. Is there something i can read to understand this more thoroughly?


Is there something i can read to understand this more thoroughly?

Practice, practice, practice. Experiment and learn. That's the best way.
The art of getting good answers lies in asking good questions.


Yes thats the way i do it. Ive never studied programming in my life..till now. All i know is from experiments, i wrote a program that generates codes to drive motors, but i cant send the commands to the arduino via serial, thus this post.


Some simple servo test code you can try using the serial monitor (captures serial character string, parses captured string into two new character strings, converts new strings into numbers, ...). You might modify this code to verify you can make the motors move via serial commands.

Code: [Select]

// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test

#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1;  // create servo object to control a servo
Servo myservo2;

void setup() {
  myservo1.attach(6);  //the pin for the servo control
  Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded

void loop() {

  while (Serial.available()) {
    delay(3);  //delay to allow buffer to fill
    if (Serial.available() >0) {
      char c = Serial.read();  //gets one byte from serial buffer
      readString += c; //makes the string readString

  if (readString.length() >0) {
      Serial.println(readString); //see what was received
      // expect a string like 07002100 containing the two servo positions     
      servo1 = readString.substring(0, 4); //get the first four characters
      servo2 = readString.substring(4, 8); //get the next four characters
      Serial.println(servo1);  //print to serial monitor to see parsed results

      int n1 = servo1.toInt();
      int n2 = servo2.toInt();

      Serial.println("the numbers are :");
      Serial.println(n1);  //print to serial monitor to see number results
      myservo1.writeMicroseconds(n1); //set servo position

Google forum search: Use Google Search box in upper right side of this page.
Why I like my 2005 Rio Yellow Honda S2000  https://www.youtube.com/watch?v=pWjMvrkUqX0

Go Up