Hello everybody,

first of all, I'm quite new to programming in C, so forgive my ignorance.
For an assignment i have to program a microcontroller (Atmega8) using ICCAVR. I'm working on a programme similar to 'simon says' (where 4 led's are illuminated in a random order, and afterwards you have to give in the same combination, using press buttons. If the combination is correct, one extra pulse is added to the combination, and so on...).

Now i'm having a bit of difficulties with reading in the array of the pressed buttons.

This is what this function looks like so far:

void receive_array()
{
int wijzer;
int a;
for(wijzer=0;wijzer<=pointer;wijzer++)
{
switch(PIND)
{

case 0b00000001:
hulparray[wijzer]=1;
PORTB = PORTB | (1 << 0);
playsound1();
break;

case 0b00000010:
hulparray[wijzer]=2;
PORTB = PORTB | (1 << 1);
playsound2();
break;

case 0b00000100:
hulparray[wijzer]=3;
PORTB = PORTB | (1 << 2);
playsound3();
break;

case 0b00001000:
hulparray[wijzer]=4;
PORTB = PORTB | (1 << 3);
playsound4();
break;

default:
PORTB=0b00000000;
}
}
}

The problem is that the programme flies over this function, without even reading in a thing. Therefore it would be handy that the programme stays inside this function, untill there's a descent flank, and then read in this value. This is necessary because otherwise the whole array would consist of the same data.

Does anybody know if this is possible? Or any othes ideas of how it should be done? All help is welcome!

Thank you

>>for(wijzer=0;wijzer<=pointer;wijzer++)
what is the value of pointer

>>switch(PIND)
Why are you using a const PIND here? Can its value ever be changed somewhere else in your program? If not, then that switch statement has no purpose.

> Why are you using a const PIND here? Can its value ever be changed somewhere else in your program?
Since the OP mentioned a micro controller, I would say this is an alias for some memory location which maps directly to a hardware switch.

> Or any othes ideas of how it should be done?
Hold down a key, then run the program.
It might show you something about your program.

what is the value of pointer

>'Pointer' is the pointer value in the 'main array', this is filled (in the beginning) ad random with int's between 1 and 4. To start, the first 3 values of this array are 'played'(pointer=2) by the LED's. After this, you have to play the same sequence by pressing the corresponding keys. This is where the 'hulparray' is filled up. the 'pointer' is thus the number of keys that should be pressed.
after this, the 'main array' and the 'hulparray' are compared: when they match, the pointer gets augmented, when they don't match, you have to start from the beginning.

Why are you using a const PIND here? Can its value ever be changed somewhere else in your program?
Since the OP mentioned a micro controller, I would say this is an alias for some memory location which maps directly to a hardware switch.

>PIND is indeed a hardware-alias directly connected to an input port, which has 8 seperate pins. These pins are connected to the keys. If key nr.1 is pressed down, 'PIND' will automatically have the value 0b00000001. If key nr. 2, it will be 0b00000010, and so on. when no key is pressed, 'PIND' will be 0b00000000.

Hold down a key, then run the program.

>When I hold down a key, and run the program, the first 3 values of the 'hulparray' are filled up with the corresponding int. That is why (according to me) the value should be read in, at the moment the key is released.

I figured it out. Instead of using only a for-loop, i put in an extra while-loop, that keeps on going, untill a key is pressed and released. i've put in an extra int (iii), that becomes 1 when a key is pressed, and becomes 2 when it is released again.

this is what it looks like now:

void receive_array()
{
 int wijzer;
 int iii;
 int a;

 	 for(wijzer=0;wijzer<=pointer;wijzer++)
 	{
  		 iii=0;

  	                while(iii!=2)
		{
		 	 switch(PIND)
			{

				case 0b00000001:
				a=1;
				iii=1;
				PORTB = PORTB | (1 << 0);
				playsound1();
				break;

				case 0b00000010:
				a=2;
				iii=1;
				PORTB = PORTB | (1 << 1);
				playsound2();
				break;

				case 0b00000100:
				a=3;
				iii=1;
				PORTB = PORTB | (1 << 2);
				playsound3();
				break;

				case 0b00001000:
				a=4;
				iii=1;
				PORTB = PORTB | (1 << 3);
				playsound4();
				break;

				case 0b00000000:
				PORTB=0b00000000;
				if ( iii==1)
				{
				iii=2;
				hulparray[wijzer]=a;
				}
				break;

				default:
				PORTB=0b00000000;
				}
			}
		}
}

And it works perfectly :-D

thx for the interest, it made me straighten my head.

commented: Excellent work +24
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.