Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help! Beginner in CodeVision AVR - How Turning On LED Based On Sensor Value?

Status
Not open for further replies.

rowan123

New Member
Hello guys, I'm a student that really new to this codevision.
Today I am here to ask some problem to a professional in this field.
Yesterday our teacher give us a proteus project and the code in codevision about ultrasonic sensor.
and showing the sensor value on the LCD on the right.
1.png

2.png
3.png
4.png


And now, our teacher tell us to add a 3 LED (red, yellow, green) on port D as an exercise.
and the requirements for the LED to blink is based on the value of the sensor.
if the value >50 the red led blink,
if the value between 31 and 50 the yellow led blink,
and if the value between 1 and 30 the green led blink.

what component do I need to put on the proteus, to make the led works?
(I already tried putting the 3 LED, resistor, and ground. do i need to put power again?)

and how to code the LED to have it works in codevision?

I already searching many articles but not finding any similar case to this.
can someone please explain it to me?
Thank you in advance.
 
At most basic level you add to schematic the 3 LEDs, each with a series R, and
connect them to a port pin and Vcc (same power as chip is using). Something like this -

1639663425379.png



The above turns them on when port bit set to 0. You could connect them from port pin
to ground then writing a 1 to port pin would tunr them on.

Then your code flows essentially like this -

Read sensor
Use case statement or if statements test value for sensor and write
to port, for that bit,/led a "0" to turn on LED. Not you should use mask
operation so you do not disturb other bits used on port apart from the LED.
Then loop back to read sensor, over and over.

Regards, Dana.
 
At most basic level you add to schematic the 3 LEDs, each with a series R, and
connect them to a port pin and Vcc (same power as chip is using). Something like this -

View attachment 134891


The above turns them on when port bit set to 0. You could connect them from port pin
to ground then writing a 1 to port pin would tunr them on.

Then your code flows essentially like this -

Read sensor
Use case statement or if statements test value for sensor and write
to port, for that bit,/led a "0" to turn on LED. Not you should use mask
operation so you do not disturb other bits used on port apart from the LED.
Then loop back to read sensor, over and over.

Regards, Dana.
Hey Dana, thank you for your reply and advice.
I have build the project as you told me (not sure if I am missing something),
led1.png

and here's the code
ledcode1.png

I'm not sure if this is right, I got no error whine compiling this code but the LED doesn't turning on on the proteus.
I put the code in void main.
I'm sorry, I'm so clueless in this field.
 
Your while() loop is done correctly, and your tests also correctly.

Do you have a nilai_sensor value that satisfies one of your tests ?

Try a super simple while( ) loop that writes a 1 to an LED bit and that all it does. No tests,
just keep writing to a bit to see if you can turn it on.

Is LED polarity right, does Proteus care about that ?

Note your LEDs turn on when a "0" is written to the bit, a "1" turns them off. If you had
connected top of LED to pin, and bottom of R to ground, then they would turn on with
a "1", if thats preferable to you.

So in short if led connected to Vcc and you write a 1 to it, a 0 to the other two bits,
you have turned off led of interest, and turned on the other two. If LED and R connected
to pin to ground, then a 1 to led of interest turns it on, the other two get turned off.

Regards, Dana.
 
Last edited:
Have you set portD to output?
Also, think about what happens when it gets to the third if with a value less than 30, it will set all pins to low. You need to use an if else structure or a switch/case structure.

Mike.
edit, the way I'd do it,
Code:
    PORTD&=0b11111000;
    if(nilai_sensor<10){
        PORTD.0=1;
    }else if(nilai_sensor<20){
        PORTD.1=1;
    }else{
        PORTD.2=1;
    }
note, whatever is in the variable one of the LEDs should always light.
 
Last edited:
Have you set portD to output?
Also, think about what happens when it gets to the third if with a value less than 30, it will set all pins to low. You need to use an if else structure or a switch/case structure.

Mike.
edit, the way I'd do it,
Code:
    PORTD&=0b11111000;
    if(nilai_sensor<10){
        PORTD.0=1;
    }else if(nilai_sensor<20){
        PORTD.1=1;
    }else{
        PORTD.2=1;
    }
note, whatever is in the variable one of the LEDs should always light.
Hi Mike...
Thanks for the reply, but it still doesn't work, i tried to change the variable of "nilai_sensor" into another possible variable "sensor_jarak", "jrk" and "hasil" but still not working.
led2.png

omg I'm hopeless!!
Just in case, I'll attach the project, please check the project & the code.
Please check in case there was an error / missing setting in my project.
Thank you.
 

Attachments

  • Ultrasonic Sensor.zip
    583.1 KB · Views: 327
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top