Thread: Simple LED script.

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    1

    Simple LED script.

    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:
    #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);
    
    
        }
    }
    A quik note, DDRB is me assigning the port it has to send a signal to, and PORTB is the signal. Since my arduino has a 5 V running trough it, PORTB = 0b00000000; means it gets turned on and PORTB = 0b10000000; means it gets turned off.

    Also this code is writen in C.

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

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    I don't know the problem, but I think it's better written like this:
    Code:
    #include <avr/io.h>
    #include <util/delay.h>
     
    int main()
    {
        while (1)
        {
            // 1Hz on, 2Hz on
            DDRB  = 0b10000000;
            PORTB = 0b00000000;
            DDRB  = 0b01000000;
            PORTB = 0b00000000;
            _delay_ms(250);
     
            // 2Hz off
            DDRB  = 0b10000000;
            PORTB = 0b10000000;
            _delay_ms(250);
     
            // 1Hz off, 2Hz on
            DDRB  = 0b10000000;
            PORTB = 0b00000000;
            DDRB  = 0b01000000;
            PORTB = 0b10000000;
            _delay_ms(250);
     
            // 2Hz off
            DDRB  = 0b10000000;
            PORTB = 0b10000000;
            _delay_ms(250);
        }
     
        return 0;
    }
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    DDRB is the "data direction register" for port B

    1 is output, 0 is input.

    Write to this once before your main loop.

    Code:
    DDRB  = 0b11000000;
    For your leds put a 500mS delay in your loop - toggle the 2Hz every loop, and every second time for the 1Hz led

  4. #4
    Registered User
    Join Date
    Dec 2017
    Posts
    1,628
    Quote Originally Posted by Click_here View Post
    For your leds put a 500mS delay in your loop - toggle the 2Hz every loop, and every second time for the 1Hz led
    Think again.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by john.c View Post
    Think again.
    Whoops - the delay should be 250mS (Thanks).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob Script Help ~ Simple Script Error
    By WiredAnomaly in forum C++ Programming
    Replies: 5
    Last Post: 10-05-2013, 05:57 AM
  2. Need help on a very simple script for college.
    By shenton in forum C Programming
    Replies: 8
    Last Post: 09-07-2012, 06:10 PM
  3. Simple shell script question.
    By Avanish Giri in forum Linux Programming
    Replies: 8
    Last Post: 04-19-2012, 02:39 PM
  4. [PAID] Very simple C script - Not working?
    By spadez in forum Projects and Job Recruitment
    Replies: 2
    Last Post: 02-12-2009, 08:00 AM
  5. help me with this simple script!
    By ryansutton in forum C++ Programming
    Replies: 2
    Last Post: 09-09-2002, 08:42 PM

Tags for this Thread