title: Atmega 32, Program to drive motor , How to take input integer from user
body: <p>I am trying to write a simple program to take input from user by hterm, when User enters "motor" &amp; "25" the motor will rotate in 25 clockwise and 25 anticlockwise direction</p>
  
  <pre><code>    //Define clock-speed and include necessary headers
      #define F_CPU 1000000
      #include &lt;avr/io.h&gt;
      #include &lt;util/delay.h&gt;
      #include &lt;inttypes.h&gt;
      #include &lt;avr/io.h&gt;
      #include &lt;stdlib.h&gt;
      #include &lt;avr/interrupt.h&gt;
      #include &lt;stdio.h&gt;
      #include &lt;stdint.h&gt;
      #include &lt;util/delay.h&gt;
      #include &lt;ctype.h&gt;
  
      #define F_CPU 16000000UL
      #define BAUD 9600UL
  
      char cmd[40];
  
      void uart_init(void)                            // initializing UART
      {
          UBRRH = 0;
          UBRRL = ((F_CPU+BAUD*8)/(BAUD*16)-1);
  
          UCSRC |= 0x86;                              // 8N1 Data
          UCSRB  = 0x18;                              // Receiving and Transmitting
      }
  
      int uart_putch(char ch, FILE *stream)           // Function for sending Data to PC
      {
          if (ch == '\n')
          uart_putch('\r', stream);
          while (!(UCSRA &amp; (1&lt;&lt;UDRE)));
          UDR=ch;
          return 0;
      }
  
      int uart_getch(FILE *stream)                    // Function for receiving Data from PC
      {
          unsigned char ch;   while (!(UCSRA &amp; (1&lt;&lt;RXC)));
          ch=UDR;
  
          uart_putch(ch,stream);                      // Echo the output back to the terminal
  
          return (tolower(ch));
  
      }
  
      FILE uart_str = FDEV_SETUP_STREAM(uart_putch, uart_getch, _FDEV_SETUP_RW);  // Important, not deleting
  
      void loeschen()                     // Delete the String
      {
          int strnglen = 0;
          while (strnglen &lt; 41 &amp;&amp; cmd[strnglen] != 0)
          {
              cmd[strnglen]= 0;
              strnglen++;
          }
      }
  
      // Define the stepping angle
      // Note: Divide by 2 if you are doing half-stepping. for filter test 1.8 defult
      #define MIN_STEP 1.8
  
      /* Define an array containing values to be sent at the required Port - for Full-stepping
  
        &lt;first four bits&gt; - &lt;last four bits&gt; = &lt;decimal equivalent&gt;
  
         00000001 = 1 ; 01000000 = 4
         00000100 = 4 ; 00010000 = 16
         00000010 = 2 ; 00001000 = 8
         00001000 = 8 ; 00100000 = 32
      */
      unsigned short control_array_full[4] = {4,16,8,32};
  
      /* Define an array containing values to be sent at the required Port - for Half-stepping
  
      &lt;first four bits&gt; - &lt;last four bits&gt; = &lt;decimal equivalent&gt;
  
      0000-1001 = 8 + 1 = 9  ; 0010-0100 = 32 + 4 =36
      0000-0001 = 1     ;      0000-0100 = 4
      0000-0101 = 4 + 1 = 5 ;  00010100 = 16 + 4 = 20
      00000100 = 4        ;   00010000  = 16
  
      00000110 = 4 + 2 = 6  ; 00011000  = 16+8=24
      0000-0010 =          ;  00-001000 = 8
      0000-1010 = 8 + 2 = 10  ; 00-101000 = 40
      0000-1000 = 8            ; 00-100000 = 32
      */
      unsigned short control_array_half[8] = {36,4,20,16,24,8,40,32};
  
  
      // Adjust this delay to control effective RPM
      // Do not make it very small as rotor will not be able to move so fast
      // Currently set at 100ms
      void delay()
      {
      _delay_ms(100);
      }
  
      void move_clockwise(unsigned short revolutions){
        int i=0;
        for (i=0; i &lt; (revolutions* 360 /MIN_STEP) ; i++)
  
        {
  
        //Note: Take modulo (%) with 8 when half-stepping and change array too
        PORTD = control_array_half[i % 4];
        delay();
       }
  
      }
  
      void move_anticlockwise(unsigned short revolutions){
       int i;
        for (i = (revolutions* 360 /MIN_STEP); i &gt; 0 ; i--){
  
        //Note: Take modulo (%) with 8 when half-stepping and change array too
        PORTD = control_array_half[i % 4];
        delay();
       }
      }
  
      int main()
      {
  
       // Enter infinte loop
       // Make changes here to suit your requirements
  
       uart_init();                   // initializing UART
  
       stdout = stdin = &amp;uart_str;        // Necessary to compare whole Strings
  
        while(1){
  
  
            scanf("%s",&amp;cmd);         // Read String from Data Register
      printf ("Please enter number of motor rotation for clockwise and anticlockwise");
            items_read = scanf ("%d", &amp;numbers[i]); // Read integer for motor revolution
  
  
            if(strcmp(cmd, "motor") == 0)
            {
                DDRD = 0b00111110; //Set PORTD  4 bits for output
         //Enter number of revolutions required in brackets
              move_clockwise(items_read);
  
              move_anticlockwise(items_read);
            }   
        DDRD = 0b00000000;
    }
          loeschen();
  }
  </code></pre>
  
  <p>Now, The problem is that when I will delete these lines from main()</p>
  
  <pre><code>  items_read = scanf ("%d", &amp;numbers[i]);
    scanf ("%d",&amp;i);
  </code></pre>
  
  <p>&amp; make items_read in <code>move_clockwise(items_read);</code> as:</p>
  
  <pre><code>move_clockwise(25);
  move_anticlockwise(25);
  </code></pre>
  
  <p>Then when user enters "motor" then motor is running <code>move_clockwise(25);</code> but <code>move_anticlockwise(25);</code> is not running, what I would like is to take both "motor", number for clockwise and number for anticlockwise....</p>
  
  <p>I would really appreciate if anyone can help me with this!
  Thanks in advance!</p>