Hello and thanks for reading. I've been working on this for a few hours now and haven't really come close to what I want to do. So basically I have 3 motors in a shield, and I want to use the serial monitor to manually set constants, which is the runtime for each motor. Ideally I would like to functionality like in python, raw_input() where it prompts for an input, and after you press enter, your value is saved and used for the rest of the program. I assumed it would be this easy but I guess it's not.
I tried to modify an example sketch to try to get 2 constants and use them to blink leds at different rates. It's just a proof of concept so it doesn't really matter to me if it works or not but the problems I'm experiencing are a). When I type in values, its very inconsistent on whether blinkrate 1 or 2 is being changed, it just seems random even though to me, the void loop() is sequential. Secondly, the integers aren't converting right while it was working fine with just 1 blink function and number input, basically the default sketch. I know the sketch is sloppy but I want to get the concept down. If anyone can shed some light or even just point me to some code where someone used the monitor to set constants that would help tremendously. Thanks for your time.
/*
* SerialReceive sketch
* Blink the LED at a rate proportional to the received digit value
*/
const int ledPin = 13; // pin the LED is connected to
const int ledPin2=11;
int blinkRate=0; // blink rate stored in this variable
int blinkRate2=0;
int value;
int value2;
////////////////////////////////////////////
void setup()
{
Serial.begin(9600); // Initialize serial port to send and receive at 9600 baud
pinMode(ledPin, OUTPUT); // set this pin as output
}
////////////////////////////////////////////
void loop()
{
getNum();
getNum2();
blink();
blink2();
}
// blink the LED with the on and off times determined by blinkRate
void blink(){
for (int i=0;i<100;i++){
digitalWrite(ledPin,HIGH);
delay(blinkRate); // delay depends on blinkrate value
digitalWrite(ledPin,LOW);
delay(blinkRate);
break;
}
}
void blink2(){
for(int i=0;i<100;i++){
digitalWrite(ledPin2,HIGH);
delay(blinkRate2);
digitalWrite(ledPin2,LOW);
delay(blinkRate2);
break;
}
}
/////////////////////////////////////////////////////////////////
void getNum() {
if( Serial.available())
{
char ch = Serial.read();
if( isDigit(ch) )// is this an ascii digit between 0 and 9?
{
value = (value * 10) + (ch - '0'); // yes, accumulate the value
}
else if (ch == 10) // is the character the newline character?
{
blinkRate = value; // set blinkrate to the accumulated value
Serial.print("here is your blinkrate 1 ");
Serial.println(blinkRate);
value = 0; // reset val to 0 ready for the next sequence of digits
}
}
}
//////////////////////////////////////////////////////////
void getNum2() {
if( Serial.available())
{
char ch2 = Serial.read();
if( isDigit(ch2) )// is this an ascii digit between 0 and 9?
{
value2 = (value2 * 10) + (ch2 - '0'); // yes, accumulate the value
}
else if (ch2 == 10) // is the character the newline character?
{
blinkRate2 = value2; // set blinkrate to the accumulated value
Serial.print("here is your blinkrate 2 ");
Serial.println(blinkRate2);
value2 = 0; // reset val to 0 ready for the next sequence of digits
}
}
}