Simple LED script.

algemene C code
Berichten: 1
Geregistreerd: 09 Sep 2021, 17:46

Simple LED script.

Berichtdoor Sticky » 09 Sep 2021, 17:49

I am taking classes in coding for arduino, for my study. However we cant simply use the program that an ardiuno comes with, we have to hard code everything. This is all fine but we got a task to make 2 LED's blink. one at 1 Hz one at 2 Hz. My idea was to have a while(1) loop with a simple counter that restarts every quarter a second. Than whenever the right number comes up turn on or of the LED.
So this is my code:
Code: Alles selecteren
#include <avr/io.h>#include <util/delay.h>


int main(void)
{
    int in_what_loop = 0;


    while(1)
    {
        if(in_what_loop == 4)
        {
            in_what_loop = 0;
        }
        in_what_loop = in_what_loop + 1;


        if(in_what_loop == 1)
        {
            DDRB = 0b10000000;
            PORTB = 0b00000000;


            DDRB = 0b01000000;
            PORTB = 0b00000000;
        }


        if(in_what_loop == 2)
        {
            DDRB = 0b10000000;
            PORTB = 0b10000000;
        }


        if(in_what_loop == 3)
        {
            DDRB = 0b10000000;
            PORTB = 0b00000000;


            DDRB = 0b01000000;
            PORTB = 0b10000000;
        }


        if(in_what_loop ==4)
        {
            DDRB = 0b10000000;
            PORTB = 0b10000000;
        }


        _delay_ms(250);


    }
}


Also this code is writen in C.

With this script only the bottom one blinks and i dont know why. Anyone know anything?

Advertisement

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Simple LED script.

Berichtdoor shooter » 10 Sep 2021, 18:15

on what pins are the leds connected and why is direction register used, just get the pin low or high.
paul deelen
shooter@home.nl

Berichten: 68
Geregistreerd: 04 Sep 2021, 08:31

Re: Simple LED script.

Berichtdoor RobGood » 11 Sep 2021, 18:26

Hi Sticky,

You are taking classes in Arduino, so you should figure this out yourself...
So tell no-one you have this from me.....

1) Do not or better use NEVER delay()
Only exception in setup routines to give hardware some time to 'setup'...

2) use simple self-made timers, using the build-in timers millis() or micros()

3) no need for the includes the compiler will add all needed links...

4) In arduino it is common to use 'Setup()' for the function called once after powerup, and loop() for the 'main()'loop.

4) this will do the trick...
mount two leds anodes (long wire) to pin 8 and 9 cathode in series with 2x 1K resister to GND.
(do not forget the resisters, otherwise the leds will blow)



unsigned long Time;

setup(){
DDRB |=(3<<0); //set pin8 and Pin9 (Portb bit 0 and bit 1 ) as outputs
}

loop(){
if(millis()-Time > 1000){ //1 second
Time=millis(); /reset timer
PINB |=(1<<0); //flip Pin8 (Port B bit 0)
GPIOR0 ^=(1<<0) // use bit0 Genral purpose register as a flag, and flip the flag
if(GPIOR0 & (1<<0)) PINB |=(1<<1)// flip Pin9 (portb bit 1)
}
}

Rob

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: Simple LED script.

Berichtdoor shooter » 12 Sep 2021, 09:49

Ja Rob je hebt helemaal gelijk,
puntje 1 in het begin is delay heel mooi hoor, maar je hebt gelijk de 2e les is al met timers.
puntje 2 is gewoon juist.
puntje 3 je zult libraries zelf moeten includen want de compiler doet dat niet automatisch hoor.
puntje 4 klopt helemaal, maar de vraag van sticky was dat de bovenste het wel doet, maar de onderste niet.
en hij gebruikt juist binaire code.
loop wordt overigens gecompileerd naar een while (1) hoor dus sticky gebruikt een iets meer naar de machinetaal gerichte oplossing maar beiden zijn juist.
(btw) if needed english just ask for it)
paul deelen
shooter@home.nl

Terug naar C code

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 10 gasten