Arduino code question

Thread Starter

Shackdaddy836

Joined Dec 13, 2010
8
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

Rich (BB code):
int i;
for (i = 10; i>=0; i--);
in void loop() but that doesn't work. I think I'm just doing something really stupid...

Thanks :)
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

Shackdaddy836

Joined Dec 13, 2010
8
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":

Rich (BB code):
 void setup(){                
  pinMode(13, OUTPUT);     //makes pin 13 an output
}

void loop(){
  digitalWrite(13, HIGH);   // sets led to high(on)
  delay(9999);              // waits for x milliseconds
  digitalWrite(13, LOW);    // sets led to low(off)
  delay(9999);              // wats for x milliseconds
}
without using any Arduino libraries. Here is what I have so far:

Rich (BB code):
void setup(){
  DDRB = B00100000;
}

void loop(){
  
  PORTB = B00100000;
  int i;
  for (i = 10; i>=0; i--);
  PORTB = B00000000;
  for (i = 10; i>=0; i--);
  
  
}
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.
 

hgmjr

Joined Jan 28, 2005
9,027
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
 

Thread Starter

Shackdaddy836

Joined Dec 13, 2010
8
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 :)

Rich (BB code):
//This is a version of the Arduino example "Blink" which comes
//with the Arduino IDE. This version doesn't use any Arduino 
//libraries.


//Pointers. We are using mDDRB and mPORTB because DDRB and PORTB is
//already defined in the Arduino IDE. So, if we REALLY want to not
//use any of the Arduino libraries, we will use different names.
uint8_t * mDDRB = (uint8_t *) 0x24;    
uint8_t * mPORTB = (uint8_t *) 0x25;    
                                       
void setup(){
 //Sets bit 5 to output.
 *mDDRB |= 0x20;                       
}

void loop(){
  //Making a volatile unsigned int for time. This can be a long if you
   //want to increase the time.
  volatile unsigned int i;             
                                      
  //Sets bit 5 to high. The LED should now be on.
  *mPORTB ^= 0x20;                     
  
  //Our delay time
  i=50000;
  //Our delay() call  
  do(i--);                             
  while(i!=0);
  
  //Sets bit 5 to LOW. The LED should be off now.
  *mPORTB ^= 0x00;                     
  
  i=50000;
  //delay()
  do(i--);                             
  while(i!=0);
  
}
 
Top