Arduino Serial Calculator


i have make calculator using serial on arduino project, we're not allowed use libraries @ all. i've coded whole thing , should work reason wont wait input @ scanf , starts loop again, me fix please.



code: [select]
static file uart00 = {0};
char function;
float value1;
float value2;
float value3;
float stored = 0;
float answer = 0;
void setup(void) {
  usart0_init(); //initialise serial parameters
}
void loop() {
  printf("enter operator:\n");
  printf("+: adds 2 values\n");
  printf("a: calculates average of 3 values\n");
  printf("!: shows factorial of given value\n");
  printf("s: squares given value\n");
  printf("m: adds stored value (default 0)\n");
  printf("r: shows stored value\n");
  printf("c: clears stored values\n");
  scanf(" %c", &function);

  switch (function) {
    case '+':
      add();
      break;
    case 'a':
    case 'a':
      mean();
      break;
    case '!':
      factorial();
      break;
    case 's':
    case 's':
      sq();
      break;
    case 'm':
    case 'm':
      memadd();
      break;
    case 'r':
    case 'r':
      readmem();
      break;
    case 'c':
    case 'c':
      clearmem;
      break;
    default:
      printf("the operation have entered invalid\n\r");
  }
}
void add ()
{
  printf("----------------|addition|----------------\n\r");
  printf("enter first value:\n");
  scanf(" %f, value1");
  printf("enter second value:\n\r");
  scanf("  %f, value2");
  answer = value1 + value2;
  printf("%f, value1");
  printf("+");
  printf("%f, value2");
  printf("=");
  printf("%f, answer\n\r");
}
void mean()
{
  printf("----------------|average|----------------\n\r");
  printf("enter first value:\n\r");
  scanf(" %f, value1");
  printf("enter second value:\n\r");
  scanf(" %f, value2");
  printf("enter third value:\n\r");
  scanf(" %f, value3");
  answer = (value1 + value2 + value3) / 3;
  printf("the mean value of \n\r");
  printf(" %f, value1,");
  printf(" %f, value2 ");
  printf("and");
  printf(" %f, value3");
  printf("is:");
  printf(" %f, answer\n\r");
}
void factorial()
{
  printf("----------------|factorial|----------------\n\r");
  printf("enter value find factorial of:\n\r");
  scanf(" %f, value1");
  if (value1 == 0)
  {
    printf("0!= 1");
  }
  else
  {
    value2 = value1;
    value3 = value2;
    while (value2 >= 2)
    {
      value2--;
      answer = value1 * value2;
      value1--;
    }
    printf("%f, value3");
    printf("! = ");
    printf("%f, answer\n\r");
  }
}
void sq()
{
  printf("----------------|square|----------------\n\r");
  printf("enter value squared\n\r");
  scanf(" %f, value1");
  value2 = value1;
  answer = value1 * value2;
  printf("%f, value1");
  printf("^2 = ");
  printf("%f, answer\n\r");
}
void memadd()
{
  printf("----------------|add stored value|----------------\n\r");
  printf("enter value added stored value\n\r");
  scanf(" %f, value1");
  value2 = value1 + stored;
  stored = value2;
  printf("%f, value1");
  printf(" has been added stored value\n\r");
  printf("the stored value = ");
  printf("%f, stored\n\r");
}
void readmem()
{
  printf("----------------|stored value|----------------\n\r");
  printf("the stored value = ");
  printf("%f, stored\n\r");
}
void clearmem()
{
  printf("----------------|clear stored value|----------------\n\r");
  stored = 0;
  printf("the stored value has been cleared\n\r");
  printf("the stored value = ");
  printf("%f, stored\n\r");
}
void usart0_init(void) {
  ucsr0a = b00000000; //single transmission speed, multiprocessor disabled
  ucsr0b = b00011000; //enable rx & tx
  ucsr0c = b00000110; //asynchronous, no parity, 1 stop, 8 bits
  ubrr0 = 103;        //load value 9600 bps baud rate whole ubrr register
  fdev_setup_stream (&uart00, tx, rx, _fdev_setup_rw); //associate rx , tx functions stdin/stdout
  stdin = stdout = &uart00 ; //the uart defined standard inputa/output device
}

static int rx(file * stream) {
  //do nothing until data received , ready read udr0; wait usart rx complete flag
  while ((ucsr0a & (1 << rxc0)) == 0) {};
  //when flag set read data usart udr0 register , return it
  return (udr0);
}

static int tx(char txdata , file * stream) {
  //do nothing until udr0 ready more data written it; wait usart udre flag
  while ((ucsr0a & (1 << udre0)) == 0) {};
  //when flag set send data placing byte udr0
  udr0 = txdata;
  return 0 ;
}

you have posted code without using code tags. code tags make code look
code: [select]
like this
when posting source code files. makes easier read, , can copied single mouse click. also, if don't it, of character sequences in code can misinterpred forum code italics or funny emoticons.  "code: [select]" feature allows select entire sketch can copied , pasted ide testing.
if have posted without using code tags, open message , select "modify" pull down menu labelled, "more", @ lower left corner of message. highlight code selecting (it turns blue), , click on "</>" icon @ upper left hand corner. click on "save" button. code tags can inserted manually in forum text using code , /code metatags.

unless sketch large, it's better if post code, rather attach it. when it's attached, have download it, create folder open code in our ide. , afterwards, folder remains unless navigate "temp" folder , manually remove it. it's easier view code in post.


Arduino Forum > Using Arduino > Programming Questions > Arduino Serial Calculator


arduino

Comments

Popular posts from this blog

Flip address is out of range arduino uno r3

Arduino Uno not uploading

Indesign and MathType fonts