Goal:
I am new to the arduino and programming enviroment and no only some basic steps. I am trying to set up my arduino to control a servo connected to a digital pin via serial inputs. I was hoping to be able to type in any angle and get my desired result.
Problems:
I am doing a little trial and error and am setting up the serial feed to display my inputs in the serial window. It seems if I type in a number like 45 or type in anything for that matter I get a response: 50, 52, 112... etc.
I have been looking a lot at the text control servo topic and found that I need to set up some sort of "charactar array" I see what they are doing yet cant comprehend their code.
I was wondering if anyone could show me the ropes with serial inputs?
any particular it is this charactar array that is my main problem. If i could simplify my problem...
I would like to type anything into the serial input such as "dog" or "123" and then have the serial respond "you sent: dog" or "you sent 123"
im currently not on my computer so i dont have my full code but if i remember my basics
#include <Servo.h>
int var;
char variable; // I still dont understand the character array and
// am still looking for some good tutorials over this
void setup ()
{
Serial.begin (9600);
Serial.println("program start"
}
void loop ()
{
if serial.available()
{var = Serial.read();
serial.println (var);
}
}
now this again not my actual code. but with my current logic this would respond with whatever i typed in the serial feed (ignoring any of my punctuation errors if any). ill keep trying tho
okay just to add to my chain of posts, i think my problems with the character array was i would perform Serial.read () and store it as an integer. i would then try to store my integer into the character array.
hopefully i'm catching my mistake. ill be able to test it in a moment
void loop()
{
if (Serial.available()) {
byte bIndex=0;
do
{
szString[bIndex++] = Serial.read();
delayMicroseconds(1000); //allow time for data to reach buffer
}while(Serial.available()&&bIndex!=c_byMaxStringLength);
//ensure the terminating \0
szString[c_byMaxStringLength]='\0';
Serial.print("You sent: ");
Serial.println(szString);
}
//othercode
}
Back when I was in seventh grade a fellow student was making fun of a friend of mine in the computer lab. He said something along the lines of "He doesn't even know what an array is." I didn't leap to my friend's defense and possibly even joined in on the mockery.
This is my penance.
An array is a -list-. It isn't anything hard to deal with at all. It is just a list of data elements all of the same type.
void setup()
{
Serial.begin(9600);
char myArray[3]; // Declare an array of characters with three elements
myArray[0] = 'a'; // This is the first element. [ index zero ]
myArray[1] = 'b'; // This is the second element. [ index one ]
myArray[2] = 'c'; // This is the third element. [ index two ]
// Note the pattern. 'Index' is 'Element minus one'
// In C arrays start being counted at the first element, or index zero.
// This has something to do with "pointer arithmetic" that you'll learn
// more about later if you stick to your studies. For now: trust that
// it DOES make sense when you have the rest of the facts in.
// Now we do tricks with the array!
for(int i=0;i<3;i++) // "For every value of _i_ starting at ZERO;
// where _i_ is LESS THAN THREE do the stuff in the braces underneath;
// and then ADD ONE to _i_
{
Serial.println(myArray[i] );
}
// The loop above should print:
//a
//b
//c
delay(2600);
for(int i=2;i>=0;i--) // "For every value of _i_ starting at TWO;
// where _i_ is GREATER THAN OR EQUAL TO ZERO do the stuff in the braces underneath;
// and then SUBTRACT ONE from _i_
{
Serial.println(myArray[i] );
}
// The loop above should print:
//c
//b
//a
}
void loop()
{
}
So: when you want to deal with an array you first need to declare the array like
;
int arrayName[100]; // 100 integers can go into this array
And then when you want to use an element in the array it is
arrayName[10] = 999; // Sets the 10th index (11th element) to 999
I've tried breaking down alphabeta's code to understand more of the components of arrays. This very simple code I wrote is giving me responses, but they are broken up.
ex: if i send "123" ... it responds " you sent 1" "you sent 2"
"you sent 3"
why did you set up the constant byte four and then add one to it?
and does the \0 mean some type of end charactar array or does it mean that sxString is not equal to zero
Try reading one character after another until you've filled up the array and then once you've got a full array print out the contents one element at a time.
why did you set up the constant byte four and then add one to it?
Bacause the way c++ knows where char strings (char arrays) ends, is to find the '\0', so wee need the string/array to have one additional index. So for later code we just use the variable. (I think that is better than always having to subtract one later in the code)
... \0 mean some type of end charactar ...
The code you posted, could lead to some bugs, the variable var; is not initiated, and could be anywhere between 0 and 255.
And you never change the value of the variable, so you will always overwrite what you've gotten before.
Did my code work for you?
BTW: the sz in szString stands for 'string zero-terminated'
just to say where i'm at the moment. I got the grasp of how serial.read takes its each variable individually and that you need to tell it when to start and stop the array...kind of.
I added to the code another piece to see what it does. now I am getting my responses in one solid phrase as intended
ex: i type "123" it responds "you sent 123#$%"
apparently this is due to how i have not added "\0" to end the array
i noticed that there is usually 3 to 4 wingding charactars that follow what i sent. is this due to that my variable is a byte and it has an extra bit for each of the characters?
char array[3];
byte count=0; //i need a counter so i can group the charactars
// set count to increase to my total array quantity
// then display what was sent
void setup ()
{
Serial.begin(9600);
Serial.println ("Start");
}
void loop ()
{ count= 0;
if (Serial.available())
{
do{
array[count++] = Serial.read();
delay(1000);
}while(Serial.available()&& count!= (3));
Serial.print("you sent");
Serial.println(array);
}
}
yes alphabeta you code works quite well, it got a little quirky tho when i put incorrect quantities of characters tho. not really a concern tho.
thank you so much for your help. Ill post again after I tweak a few more of my own quirks
char array[4]; //make space for the terminating \0
byte count=0; //i need a counter so i can group the charactars
// set count to increase to my total array quantity
// then display what was sent
void setup ()
{
Serial.begin(9600);
Serial.println ("Start");
}
void loop ()
{ count= 0;
if (Serial.available())
{
do{
array[count++] = Serial.read();
delay(1000);
}while(Serial.available()&& count!= (3));
array[count]='\0'; //now Serial.prinln(array); should stop at 3rd char.
Serial.print("you sent");
Serial.println(array);
}
}
(almost quote)
serial.read takes each variable individually so you need to tell it when to start and stop putting variables into the array
fjornir
I'd like to attempt what you said about making it wait for me to fill up the array and then let it send it back to me individually, yet I've hit a snag attempting this I dont know exactly how to pull it off. I think Im in need of a hint to get me going.
alphabeta
here is my recreation of your code. I think I understand the most of it. I got rid of the wingdings after my numbers and it seems to work to me.
I was wondering how could I make the computer not send my a response if i put in an incorrect amound of characters into the array.
right now if i put only one charactar in such as "5" it responds "you sent 5 " or if i put in "1234" it responds "you sent 123" "you sent4".
how could i make it ignore an invalid entry like those.
heres my code. Please spot out what seems like its erronous or might be problematic.
char array[3];
byte count=0; //i need a counter so i can group the charactars
byte three = 3; // set count to increase to my total array quantity
// then display what was sent
void setup ()
{
Serial.begin(9600);
Serial.println ("Start");
}
void loop ()
{ count= 0;
if (Serial.available())
{
do{
array[count++] = Serial.read();
delay(1000);
}while(Serial.available() && count < three);
array[count] = '\0';
Serial.print("you sent ");
Serial.println(array);
}
finally my main goal was to use an entry of degrees to control a servo. i want to be able to type in 110 and watch the servo position itself at 110 degrees. Will it be possible to just use my charactar array like a variable and make the servo = array.
or something of the sort. again thanks so much i've been able to accomplish a lot today
hmm. clearly you can not pretend an array and integer are the same thing. how can i convert this array input into an integer so my servo can read it. hmmmmmm..
i've done way more then what i imagined i would tonight. I was able to take the serial program i had goind and thanks to the "text control servo" pages additional help turned the array into an integer.
the motor responds great to my positioning.
posting it just to post it
#include <Servo.h>
#include <stdlib.h> // this is for atoi for converting array into int
Servo servo1; //creates servo object to control a servo
char array[3];
byte count=0; //i need a counter so i can group the charactars
byte three = 3; // set count to increase to my total array quantity
int servo; // then display what was sent
void setup ()
{
Serial.begin(9600);
Serial.println ("Start");
servo1.attach (9);
}
void loop ()
{ count= 0;
if (Serial.available())
{
do{
array[count++] = Serial.read();
delay(1000);
}while(Serial.available() && count < three);
array[count] = '\0';
servo = atoi(array);
Serial.print("you sent ");
Serial.println(array);
}
servo1.write(servo);
}
I tried your method of having the program read each charactar individually and send them back out one at a time. I got this to work and was interested if I could create an array and break it into chunks.
for ex: a four character array,
I would make it read all four characters and then how could I group and display the last two characters? sorry for asking so many questions... I need to find some good c++ programming books