Arduino code question

Discussion in 'Programmer's Corner' started by Shackdaddy836, Aug 29, 2011.

  1. Shackdaddy836

    Thread Starter New Member

    Dec 13, 2010
    8
    0
    Hey, I'm new to Arduino and was wondering, what is the equivalent code to delay()? Like if I didn't use any Arduino libraries then what would the code look like.

    I tried

    Code ( (Unknown Language)):
    1.  
    2. int i;
    3. for (i = 10; i>=0; i--);
    4.  
    in void loop() but that doesn't work. I think I'm just doing something really stupid...

    Thanks :)
     
  2. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    What is the clue that your "for loop" is not working?

    The code looks proper to me. Of course with an initial value of 10 the delay loop is going to be very short indeed.

    hgmjr
     
  3. Shackdaddy836

    Thread Starter New Member

    Dec 13, 2010
    8
    0
    Ok. Maybe it's something different in my sketch. Let me clarify my question. My goal for the whole sketch is to make a version of the Arduino example "blink":

    Code ( (Unknown Language)):
    1.  void setup(){                
    2.   pinMode(13, OUTPUT);     //makes pin 13 an output
    3. }
    4.  
    5. void loop(){
    6.   digitalWrite(13, HIGH);   // sets led to high(on)
    7.   delay(9999);              // waits for x milliseconds
    8.   digitalWrite(13, LOW);    // sets led to low(off)
    9.   delay(9999);              // wats for x milliseconds
    10. }
    11.  
    without using any Arduino libraries. Here is what I have so far:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. void setup(){
    4.   DDRB = B00100000;
    5. }
    6.  
    7. void loop(){
    8.  
    9.   PORTB = B00100000;
    10.   int i;
    11.   for (i = 10; i>=0; i--);
    12.   PORTB = B00000000;
    13.   for (i = 10; i>=0; i--);
    14.  
    15.  
    16. }
    17.  
    What happens is that when I send it to my board, the light (on pin 13) appears to be dim. It doesn't blink or anything. I've tried different values for 'i' too but it doesn't help. It is't the board because I've tried the example that includes the library and it works so it must be something I'm doing wrong either by messing up my for loop or by messing up with writing to the registers.
     
  4. srikanthsamaga

    Member

    Jul 1, 2008
    13
    1
    The LED is blinking very fast, so it appears to be dim. You need to give a very large value for i
     
  5. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Declare the type for i to be "UNSIGNED LONG". That way you can do as srikanthsamaga has recommended and use much larger values of i in the "for loop".

    You probably have declared i of type "CHAR". That only gives you the maximum value of 255.

    hgmjr
     
  6. Shackdaddy836

    Thread Starter New Member

    Dec 13, 2010
    8
    0
    Thanks everyone for helping. I figured it out. I was waaaay off on my code :\. I will post my working code here for future reference :)

    Code ( (Unknown Language)):
    1.  
    2. //This is a version of the Arduino example "Blink" which comes
    3. //with the Arduino IDE. This version doesn't use any Arduino
    4. //libraries.
    5.  
    6.  
    7. //Pointers. We are using mDDRB and mPORTB because DDRB and PORTB is
    8. //already defined in the Arduino IDE. So, if we REALLY want to not
    9. //use any of the Arduino libraries, we will use different names.
    10. uint8_t * mDDRB = (uint8_t *) 0x24;    
    11. uint8_t * mPORTB = (uint8_t *) 0x25;    
    12.                                        
    13. void setup(){
    14.  //Sets bit 5 to output.
    15.  *mDDRB |= 0x20;                      
    16. }
    17.  
    18. void loop(){
    19.   //Making a volatile unsigned int for time. This can be a long if you
    20.    //want to increase the time.
    21.   volatile unsigned int i;            
    22.                                      
    23.   //Sets bit 5 to high. The LED should now be on.
    24.   *mPORTB ^= 0x20;                    
    25.  
    26.   //Our delay time
    27.   i=50000;
    28.   //Our delay() call  
    29.   do(i--);                            
    30.   while(i!=0);
    31.  
    32.   //Sets bit 5 to LOW. The LED should be off now.
    33.   *mPORTB ^= 0x00;                    
    34.  
    35.   i=50000;
    36.   //delay()
    37.   do(i--);                            
    38.   while(i!=0);
    39.  
    40. }
    41.  
     
Loading...