Help with cases

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, I'm really new to this thing and I would appreciate some guidance.

I have this project using atmega328p I have to test how many times light-bulb was turned on/off but that cycle have tu programmable and I need to change parameters for at least two bulbs.

I have four buttons, one to start test, one to select which bulb cycle I'm changing, one to select how long it will be turned on and last one how long it will be turned off.

 

This is the code I was playing around with:


#include <avr/io.h>
#include <avr/interrupt.h>
#include "LCD.H" //including LCD library
#include <stdio.h>
#include <util/delay.h>
#define F_CPU 16000000

volatile unsigned int PL;
volatile unsigned int PL2;

#define BUTTON1 	 21  // Button 1
char but;   //button state
char butl;  //button last pass
char butos; //button presed oneshot
char press; //0,1,2,3

int main(void)
{
	DDRD = 0xFF; // PORTD output
	PORTD =0; // clear PORTD
	PORTC =0; // clear PORTC

	EICRA |= (1 << ISC00) || ( 1 << INT1 ) ;    // set INT0 and INT1 to trigger on ANY logic change
	EIMSK |= (1 << INT0) || ( 1 << INT1 );        // Enable both interrupts
	sei();               // Enable global interrupts

	but=BUTTON1;        //read button
	butos=but && !butl; //generate oneshot
	butl=but;           //remember last pass
	if(butos){
		press++;
		if(press > 1) press=0;
	}
	switch(press){
		case 0: OCR1A=0;   break;
		case 1: OCR1A=64;  break;

	}
	_delay_ms(20); //longer than bounce time, so no bounce problemo

	while (1)
	{
		printf("On/off: %i", PL); // Print out the total pulses
	}
}

ISR(INT0_vect)
{
	PL++;       // Increment the pulse counter by one each time
}

ISR(INT1_vect)
{
	PL2++;       // Increment the pulse counter by one each time
}

I select a bulb by a number of times I press a button with the help of cases.

What I don't know is can I expand upon those cases, for example: I press one button two times to select second bulb, then I press second button three times to select cycle time.  Do I add case within case(if so how? because I tried and just got a bunch of errors) or am I going in the completely wrong direction here?

Would be really thankful for some help.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
char press; //0,1,2,3

Err no. You have:

		press++;
		if(press > 1) press=0;

press can never be anything but 0.

 

Also this looks very suspicious too:

#define BUTTON1 	 21  // Button 1
char but;   //button state
char butl;  //button last pass

...

	but=BUTTON1;        //read button
	butos=but && !butl; //generate oneshot
	butl=but;           //rem

At start up but1 is 0 so this code says:

	but=21;        //read button
	butos=0 && 0; //generate oneshot
	butl=but;           //rem

!21 is 0. 0 && 0 is 0. So by the end of that all of butos = 0. To be honest I can't actually work out what you had in mind but I can't help thinking it's not this. Do you really mean ! and not ~? Do you really mean && and not & ?

 

Then to top everything else you have all this code OUTSIDE the while(1) so all this code runs only once in the few microseconds after you power on the AVR and then it just holds in the while(1) doing nothing but printing and waiting for INT0. (the INT1 code is irrelevant and nothing ever uses PL2)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hirajus wrote:

#include <util/delay.h>
#define F_CPU 16000000

F_CPU's definition should appear BEFORE the delay.h include.

 

Ross McKenzie ValuSoft Melbourne Australia