Timer

Hmm, I guess the mega8 doesn’t have a divide-by-8 clock fuse like the mega168 does.

- Ben

Your roulette code doesn’t activate the internal pull-up resistor on PC5, so it always thinks the button is being pressed and jumps right to the “spin the wheel” function.

Try changing the line:
PORTC = 0;
to:
PORTC|=(1<<PC5);

Also, you need to create the pot_idx variable once outside of the rest of the loops, like this:

unsigned char i,pot_idx=0;
while(1)
      {



// Initialize the ADC:
      analog_init();


// The endless loop
   while(PINC&(1<<PC5)){//while button PC5 is not pressed
     // scale the potentiometer ADC value (0 - 255) to be a number from 0 - 36
     pot_idx=(unsigned char)(analog8(7)*36/255);

     lcd_line2();
     lcd_int(pot_idx);
     
     _delay_ms(25);
   }
   
   pot_idx=single_zero_order[pot_idx];
   
   while(pot_idx<=37){
     lcd_line2();
     lcd_int(single_zero_wheel[pot_idx]);
     
     pot_idx++;//increment pot_idx
     
     for(i=0;i<10;i++){
       _delay_ms(25);
     }

}
}

That way you only have to create it once, and it can pass the user’s selected number to the wheel-spin part of the main function.

And yes, to read from ADC6 instead of ADC7 you should be able to just call “analog8(6)”.

-Adam

Thanks for help. In your part of the code that converts the number from single_zero_order to single_zero_wheel, have you made it randomly show some numbers? because when i press PC5, it displays loads of numbers one after the other, but doesnt stop on the equivelent one from the wheel array. I changed the code slightly so that the wheel array, would appear on the top line, so i could still see the number i chose.

 while(pot_idx<=37){
     lcd_line1();
     lcd_int(single_zero_wheel[pot_idx]);
     
     pot_idx++;//increment pot_idx
     
     for(i=0;i<10;i++){
       _delay_ms(25);
     }

What would have to be done so that when PC5 was pressed, (on bottom line of LCD) the top line displays all numbers (next to each other) that come after the chosen wheel number, not really a game of roulette anymore…

The other thing is, the code for the interupts thingy, i think for the second timing (2nd to 3rd button press gap) it is adding the first timing (1st to 2nd button press timing), because everytime it is almost double the first one…

int time(){
   while (PINC&(1<<PC5));  // loop here until first button press

   while (1){  // loop forever
      _delay_ms(10);  // delay for 10 ms to debounce the press
      
      while (!(PINC&(1<<PC5)));  // loop here until button is released
      
      _delay_ms(10);  // delay for 10 ms to debounce button release
      
      time_ms1 = 0;  // here is where we start timing
      time_ms2 = 0;
      TCNT0 = 10;
      
      
      while (PINC&(1<<PC5));  // loop here until second button press
      time_ms1 = timer_tick;      //get the value of the millisecond counter
            
      while (!(PINC&(1<<PC5)));  // loop here until button is released
      
      _delay_ms(10);  // delay for 10 ms to debounce button release
      
      TCNT0 = 10;
      while (PINC&(1<<PC5));  // loop here until 3rd button press

      time_ms2 = timer_tick;      //get the value of the millisecond counter
      
      lcd_line1();// display the times
      lcd_time(time_ms1);
      lcd_line2();
      lcd_time(time_ms2); 
   }
}

Sorry, just trying to iron out some little confusions…

I guess I still don’t understand what you want to be displayed when you press the button in the roulette program, since the lines are only 8 characters wide.

Just looking at your timer code breifly, I see a couple of slight improvements you could make by rearranging the order of lines a little bit, and setting TCNT0 to 0, not 10 (I think that’s a leftover from the 1MHz pure delay code):

int time(){
   time_ms1 = 0;  
   time_ms2 = 0;

   while (1){  // loop forever
	  while (PINC&(1<<PC5));  // loop here until first button press
	  TCNT0 = 0;
	  timer_tick=0;// here is where we start timing
	  _delay_ms(1);  // delay for 1 ms to debounce button press

	  while (!(PINC&(1<<PC5)));  // loop here until button is released
	  _delay_ms(10);  // delay for 10 ms to debounce button release
	 
	 
	  while (PINC&(1<<PC5));  // loop here until second button press
	  time_ms1 = timer_tick;      //get the value of the millisecond counter
	  _delay_ms(1);  // delay for 1 ms to debounce button press
		   
	  while (!(PINC&(1<<PC5)));  // loop here until button is released
	 
	  _delay_ms(10);  // delay for 10 ms to debounce button release
	 
	  while (PINC&(1<<PC5));  // loop here until 3rd button press
	  time_ms2 = timer_tick;      //get the value of the millisecond counter
	 
	  lcd_line1();// display the times
	  lcd_time(time_ms1);
	  lcd_line2();
	  lcd_time(time_ms2);
   }
}

This timing will go by when you press the button, rather than when you release it. I still wouldn’t be surprised if you still see similar time offsets though. Your internal RC oscillator is only 3% accurate at best, so it could actually be oscillating at 8.24MHz, and your 10 seconds would be measured as 10.3 seconds. The rest could be a combination of temperature drift of the oscillator, and the limitations of human reflexes.

-Adam

this is what happens:

  1. press button - start timer
  2. timer running - recorded time gap a, c, e
  3. press button stop timer - and start new timer
  4. timer running - recorded time gap b, d, f
  5. press button - stop timer2
  6. display times

Timing 1, held for 5 real seconds each
time a: 5.26 s
time b: 11.38 s

held for 10 real seconds each
time c: `10.33 s
time d: 19.28 s

I think what is happening is the timers value is not being reset after the first timing, so the second one starts at the time for the second 1 is almost double the first???

For the roulette,

  1. Turn pot,
  2. Choose number, - second line
  3. display all numbers in array after the chosen number (in order of roulette wheel) - on both lines if necessary.

I am thinking about adding in my crystal oscillator… i have a circuit for that

OOOoh, yeah, I thought you were trying to do a split-style timer. If you want to reset the time (like a lap-timer) you would just need to re-zero timer_tick at the second button press, like this:

int time(){
   time_ms1 = 0; 
   time_ms2 = 0;

   while (1){  // loop forever
	 while (PINC&(1<<PC5));  // loop here until first button press
	 TCNT0 = 0;
	 timer_tick=0;// here is where we start timing
	 _delay_ms(1);  // delay for 1 ms to debounce button press

	 while (!(PINC&(1<<PC5)));  // loop here until button is released
	 _delay_ms(10);  // delay for 10 ms to debounce button release
   
   
	 while (PINC&(1<<PC5));  // loop here until second button press
	 time_ms1 = timer_tick;      //get the value of the millisecond counter
     timer_tick=0;// here is where reset the timer
	 _delay_ms(1);  // delay for 1 ms to debounce button press
		 
	 while (!(PINC&(1<<PC5)));  // loop here until button is released
   
	 _delay_ms(10);  // delay for 10 ms to debounce button release
   
	 while (PINC&(1<<PC5));  // loop here until 3rd button press
	 time_ms2 = timer_tick;      //get the value of the millisecond counter
   
	 lcd_line1();// display the times
	 lcd_time(time_ms1);
	 lcd_line2();
	 lcd_time(time_ms2);
   }
}

To display all the remaining numbers on the roulette wheel just move the “lcd_line2();” command up, just before the “while(pot_idx<=37)” loop. It’s going to run off of the screen pretty quickly though.

-Adam

hey, unless i’m being stupid, the lcd_line2(); command is before while(pot_idx<=37){…

    lcd_line2();
     lcd_int(pot_idx);
     
     _delay_ms(25);
   }
   
   pot_idx=single_zero_order[pot_idx];
   
   while(pot_idx<=37){
     lcd_line1();
     lcd_int(single_zero_wheel[pot_idx]);
     
     pot_idx++;//increment pot_idx
     
     for(i=0;i<10;i++){
       _delay_ms(25);
     }

how could i get it to appear for say 10 seconds… could i run a function based on the numbers that follow the number chosen…
also for the timer, how can i send it back to the “main menu” after a delay, so that i can choose another option…

Whoops, I guess I was looking at an older version of your code.

If you want to display the remaining wheel numbers across a line rather than just flashing them you can move the “lcd_line1();” command up above “while(pot_idx<=37)”. That way the LCD cursor it won’t keep going back to the beginning of the line before writing out a new number. Unfortunately the screen is going to fill up after like the third number.

Do you still have that delay_sec function in your program? You could totally use that to generate 10 second delays:

void delay_sec(unsigned char sec){
	unsigned int cycles;

	for(cycles=0;cycles<(sec*100);cycles++){
		_delay_ms(10);
	}
}

And if there’s a part of the code you want to always come back to, I would wrap everything you want to repeat (not the “initialize functions”) up in a while(1) loop, so when everything else is done the code returns to the beginning. Remember, you can jump out of any loop with the “break;” command.

-Adam

sweet everything works now. I am going to move my learning project on now, to get it to say the numbers…

I have a template program but the one piece i cant find anywhere to buy is the vs1003b mp3 decoder? have you seen it anywhere, sparkfun dont have it…

Also please can you help me with this list of components, I think i have it pretty close, but not 100%. The link below is the same link to when i was making the breadboard power supply, but at the top are two links. 1 to the schematic, and 1 to my list of parts…

Cheers

Glad to hear everything’s working.

Are you going ahead with your MP3 player project then, good luck! There are a lot of slight variations on the VS??? mp3 decoder chip, I don’t see anywhere that specifically carries the VS1003b though. Check out the manufacturer’s website, the different models may be interchangeable (or not).

I don’t see the links you’re referring to though…

-Adam

I’m an idiot, didnt post the link huh…
ksd-design.co.uk/photos/photos.html
thats the mp3 player project started.

Quick question about my roulette pot.

I have wired from ADC6 5 volts to a 10k resistor, that goes into point on slide potentiometer, other end goes in ground, and the variable bit goes into pin reading…

as a slide the pot, the numbers do change but dont cover the whole range, they only move from 27/28 to 36, how do i get them to cover all numbers?

thanks

alex

You don’t need to use any extra resistor, just the potentiometer alone.

Where did you get the schematic? From it alone I don’t see how you can tell the actual value of all the “104” capacitors.

-Adam

hey, i emailed the guy who drew the schematic, apparently cap 104 is 0.1 uf… are they harder to get hold of than other capacitors? If i got ametallised polyester capacitor instead of radial electrolytic, what difference would it make?
I am trying to build a shopping basket of bits, and there is 3 things i cant find

U1||ASM1117-3.3|
U2||ASM1117-2.5|
I think they are voltage regulators, but I;m not sure what i can get away with…

JP1|Header, 2-Pin|Header 2|
Does this mean a way of connecting power to the circuit?

and lastly

C14|Capacitor|30p|
R5|Resistor|Res1|510
I have 560 and 470 ohm resistors, are they close enough? and 30 peco farads thats tiny???

the website is rapidonline.com is there any chance you may be able to take a look?

No, they’re quite common. They’re good to place near sensitive inputs and outputs to smooth out signals. 100uF, 10uF, and 0.1uF are probably the three most common capacitor sizes in hobby electronics, and I see that schematic calls for some 1uF caps as well. Time to stock up!

-Adam

Ah, i added more to that post not realising you already answered it…

do these have to be put into a circuit the correct way round, and three in a row = 30pf?

maplin.co.uk/module.aspx?Mod … 7&doy=15m5

Yeah, U1 is a 3.3V regulator and U2 is a 2.5V regulator. You don’t have to use those specific parts (I can’t seem to find datasheets for them), you can use regulators available from Rapid. In the regulator part of the circuit you should use whatever capacitors are recommended in the datasheets of the regulators you do get.

JP1 is just two header pins, so yes, it’s just a connector to whatever your power source is, not a specific component.

Yes, you absolutely do need tiny capacitors of some sort for C14. Component X1 is a 12.288 MHz crystal oscillator, and it will oscillate alright by itself, but it will damage itself over time if it doesn’t have the capacitors there. You should use whatever capacitors are recommended in the datasheet of the crystal you go with. You can also pick out a three pin ceramic resonator with internal capacitors if you want.

The 510 ohm resistors are just current limiting for the LEDs, so they don’t need to be super-precise.

-Adam

Those regulators we talked about once before, the 5 volt ones, were huge, you can get smaller than those cant you? should i still get 2.5 and 3.3 volt ones though? The ones in the schematic are also 4 pin, but can i use 3 pin like i was before and not connect up Vo

Yeah, this circuit shouldn’t require much power, and you can find 100mA regulators in much smaller packages. By the way, if you’re planning on building this on a breadboard you’re going to need a DIP breakout board for the mp3 decoder chip (not to mention a bigger breadboard).

-Adam

P.S. Capacitors add in parallel, so to get 30pF you would put three 10pF capacitors next to each other, not in a row. Resistors however add in series (in a row). Rather than doing that I would just buy the closest valued single resistor you can find.

What a 30Peco ohm resistor instead of capacitor?

How do i work out what size breakout board i need? on a permanent circuit, whats the equivelent of a dip board?

No, resistors and capacitors are not interchangeable.

The total capacitance of a group of capacitors is only the sum of their individual capacitances when they are connected in parallel:

If you connect capacitors in a row (in series) the total capacitance will actually be less than the capacitance of the smallest capacitor:

So yes, you can use three 10pF capacitors instead of one 30pF capacitor, but you will need to connect the capacitors up in parallel (next to each other) not in series (in a row).

The only reason I mentioned resistors is that, interestingly, they work the other way around. Resistances add in series and reduce in parallel:

If you like you can plot out every connection you’re going to make on graph paper to figure out how big a breadboard you’re going to need. If you later want to move your breadboard circuit to a more permanent home, you can solder it to a copper-clad perfboard like this one. You might need to make a few positioning changes, and you will need to connect together all the power/ground pins yourself with wires.

-Adam