can I generate a random number between 0 to 6? do I have to link some C libs to do that, or can I just use rnd function?
If you do an
#include <stdlib.h> you will have access to the C function random(), which is documented here.
When compiling code with this function, you need to link in the math library by adding the -lm option to the line in the Makefile where the .obj file is created.
If you execute random()%7 repeatedly, you’ll get a nice random sequence of numbers between 0 and 6. However, you will get exactly the same “random” sequence every time you reset your Orangutan, which might not be what you want. If you want the sequence to be different every time, you need to “seed” the random number generator with some truly random data. If your program requires user input, like a button press, to begin, you can time the length of the button press very accurately to get this random seed. Get the time that the button was down, say, in microseconds, and pass it the function srandom(time).
What are you using the random numbers for?
I want my robot to go to random locations on a grid, so I need to generate the x and y’s.
I am using avrdude on ubuntu. do I need to add the -lm to the makefile?
I guess I do…
this is my make file. I got 2 obj parts so where do I add the -lm?
CC=/usr/bin/avr-gcc MEGA=168 CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=atmega$(MEGA) OBJ2HEX=/usr/bin/avr-objcopy PROG=/usr/bin/avrdude TARGET=gridder program : $(TARGET).hex $(PROG) -c avrispmkII -p m$(MEGA) -P usb -e $(PROG) -c avrispmkII -p m$(MEGA) -P usb -U flash:w:$(TARGET).hex %.obj : %.o $(CC) $(CFLAGS) $< -o $@ $(TARGET).obj : $(TARGET).o pwm.o servo.o lcd.o buzzer.o $(CC) $(CFLAGS) $(TARGET).o pwm.o servo.o lcd.o buzzer.o -o $@ %.hex : %.obj $(OBJ2HEX) -R .eeprom -O ihex $< $@ clean : rm -f *.hex *.obj *.o