How to use serial monitor to input initial set of constants?


               hello , reading.  i've been working on few hours , haven't come close want do.  have 3 motors in shield, , want use serial monitor manually set constants, runtime each motor.  ideally functionality in python, raw_input() prompts input, , after press enter, value saved , used rest of program.  assumed easy guess it's not.
              tried modify example sketch try 2 constants  , use them blink leds @ different rates.  it's proof of concept doesn't matter me if works or not problems i'm experiencing a).  when type in values, inconsistent on whether blinkrate 1 or 2 being changed, seems random though me, void loop() sequential.  secondly, integers aren't converting right while working fine 1 blink function , number input, default sketch.  know sketch sloppy want concept down.  if can shed light or point me code used monitor set constants tremendously.  time.

code: [select]
/*
* serialreceive sketch
* blink led @ rate proportional received digit value
*/
const int ledpin = 13; // pin led connected to
const int ledpin2=11;
int   blinkrate=0;     // blink rate stored in variable
int   blinkrate2=0;
int value;
int value2;
////////////////////////////////////////////
void setup()
{
  serial.begin(9600); // initialize serial port send , receive @ 9600 baud
  pinmode(ledpin, output); // set pin output
}

////////////////////////////////////////////
void loop()
{
  getnum();
  getnum2();
  blink();
  blink2();
}

// blink led on , off times determined blinkrate
void blink(){

  (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) )// ascii digit between 0 , 9?
    {
       value = (value * 10) + (ch - '0'); // yes, accumulate value
    }
    else if (ch == 10)  // character newline character?
    {
       blinkrate = value;  // set blinkrate accumulated value
       serial.print("here blinkrate 1 ");
       serial.println(blinkrate);
       value = 0; // reset val 0 ready next sequence of digits
    }
  }
}
//////////////////////////////////////////////////////////
void getnum2() {
   if( serial.available())
  {
    char ch2 = serial.read();
    if( isdigit(ch2) )// ascii digit between 0 , 9?
    {
       value2 = (value2 * 10) + (ch2 - '0'); // yes, accumulate value
    }
    else if (ch2 == 10)  // character newline character?
    {
       blinkrate2 = value2;  // set blinkrate accumulated value
       serial.print("here blinkrate 2 ");
       serial.println(blinkrate2);
       value2 = 0; // reset val 0 ready next sequence of digits
    }
  }
}

             

what have line ending set in serial monitor ?

as aside, setting variables, not constants, not cause of problem, merely misuse of word constant in context.


Arduino Forum > Using Arduino > Programming Questions > How to use serial monitor to input initial set of constants?


arduino

Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62