Arduino Photo resistor ( lights the LED when dark & dims the LED when there is light)











up vote
0
down vote

favorite
1












sbi DDRB, 5 //led output
cbi DDRC, 0 //LDR input
sbi PORTC, 0 //pull up resistor

//---- Conversion starts here ----//
LDI R16 , 0x87
STS ADCSRA, R16
LDI R16 , 0x11

STS ADMUX, R16
//configurations done

READ_ADC:
//CBI PORTB,5
LDS R18, ADCSRA
SBR R18,0b01000000
STS ADCSRA,R18

KEEP_POLING :
LDS R19, ADCSRA
SBRS R19, 4
//
RJMP KEEP_POLING // loop bc conversion not complegted ADIGF =0+


LDS R16, ADCSRA
SBR R16,0b00010000
STS ADCSRA,R16

// above is setting bit 4 to 1 indicating that conversion over

//below is reading the high and low byte of the ADC
LDS R16,ADCL //8 bits
LDS R17,ADCH // read 2 bits


LDI R20, 0Xf4
CP R16,R20 //IF R16>=R20
BRGE greateq // The low bits are greater than 244 so if ADCH has a value of one which is 2^8=256+244=500
CP R20,R16 //IF R20> R16 (It's dim)
BRGE check

greateq:
LDI R21,0X01 //Here we are checking if it is greater than or equal to one (2^8 on or more)
CP R17,R21
brge light
CP R21, R17
brge dim

check: // The ADCL has a decimal value lower than 244 so we need either 2^9 or both 2^8 and 2^9 but ( 2^8 alone is not enough )
LDI R23,0x02 // Greater than or equal to 2 aka either (2^9 is on) or (2^8 and 2^9 together are on)
CP R17,R23
brge light
CP R23, R17
brge dim

light:
CBI PORTB, 5 // Turns off the LED

dim:
SBI PORTB, 5 // Turns on the LED

RJMP READ_ADC


The problem is that the LED always stays on.
The code is supposed to keep on checking whether the light intensity has exceeded 500 or not and if it did it should clear the bit which the LED is connected to , but if there is a light intensity more than 500 it should set the bit to 1 to light up the LED.
What is messing up the values in my code?










share|improve this question




















  • 1




    These kind of problems are easier understood when a schematic is also attached. Should we assume the circuit is correct? You are driving the LED directly from the ATMega256p output pin and sensing the photo-resistor by ADC'verting the drop on it when put in a voltage divider, right? The cbi portb, 5 falls through to sbi portb, 5 that will undo the clear. Are you missing a rjmp portb, 5 before the label dim?
    – Margaret Bloom
    Nov 11 at 10:58















up vote
0
down vote

favorite
1












sbi DDRB, 5 //led output
cbi DDRC, 0 //LDR input
sbi PORTC, 0 //pull up resistor

//---- Conversion starts here ----//
LDI R16 , 0x87
STS ADCSRA, R16
LDI R16 , 0x11

STS ADMUX, R16
//configurations done

READ_ADC:
//CBI PORTB,5
LDS R18, ADCSRA
SBR R18,0b01000000
STS ADCSRA,R18

KEEP_POLING :
LDS R19, ADCSRA
SBRS R19, 4
//
RJMP KEEP_POLING // loop bc conversion not complegted ADIGF =0+


LDS R16, ADCSRA
SBR R16,0b00010000
STS ADCSRA,R16

// above is setting bit 4 to 1 indicating that conversion over

//below is reading the high and low byte of the ADC
LDS R16,ADCL //8 bits
LDS R17,ADCH // read 2 bits


LDI R20, 0Xf4
CP R16,R20 //IF R16>=R20
BRGE greateq // The low bits are greater than 244 so if ADCH has a value of one which is 2^8=256+244=500
CP R20,R16 //IF R20> R16 (It's dim)
BRGE check

greateq:
LDI R21,0X01 //Here we are checking if it is greater than or equal to one (2^8 on or more)
CP R17,R21
brge light
CP R21, R17
brge dim

check: // The ADCL has a decimal value lower than 244 so we need either 2^9 or both 2^8 and 2^9 but ( 2^8 alone is not enough )
LDI R23,0x02 // Greater than or equal to 2 aka either (2^9 is on) or (2^8 and 2^9 together are on)
CP R17,R23
brge light
CP R23, R17
brge dim

light:
CBI PORTB, 5 // Turns off the LED

dim:
SBI PORTB, 5 // Turns on the LED

RJMP READ_ADC


The problem is that the LED always stays on.
The code is supposed to keep on checking whether the light intensity has exceeded 500 or not and if it did it should clear the bit which the LED is connected to , but if there is a light intensity more than 500 it should set the bit to 1 to light up the LED.
What is messing up the values in my code?










share|improve this question




















  • 1




    These kind of problems are easier understood when a schematic is also attached. Should we assume the circuit is correct? You are driving the LED directly from the ATMega256p output pin and sensing the photo-resistor by ADC'verting the drop on it when put in a voltage divider, right? The cbi portb, 5 falls through to sbi portb, 5 that will undo the clear. Are you missing a rjmp portb, 5 before the label dim?
    – Margaret Bloom
    Nov 11 at 10:58













up vote
0
down vote

favorite
1









up vote
0
down vote

favorite
1






1





sbi DDRB, 5 //led output
cbi DDRC, 0 //LDR input
sbi PORTC, 0 //pull up resistor

//---- Conversion starts here ----//
LDI R16 , 0x87
STS ADCSRA, R16
LDI R16 , 0x11

STS ADMUX, R16
//configurations done

READ_ADC:
//CBI PORTB,5
LDS R18, ADCSRA
SBR R18,0b01000000
STS ADCSRA,R18

KEEP_POLING :
LDS R19, ADCSRA
SBRS R19, 4
//
RJMP KEEP_POLING // loop bc conversion not complegted ADIGF =0+


LDS R16, ADCSRA
SBR R16,0b00010000
STS ADCSRA,R16

// above is setting bit 4 to 1 indicating that conversion over

//below is reading the high and low byte of the ADC
LDS R16,ADCL //8 bits
LDS R17,ADCH // read 2 bits


LDI R20, 0Xf4
CP R16,R20 //IF R16>=R20
BRGE greateq // The low bits are greater than 244 so if ADCH has a value of one which is 2^8=256+244=500
CP R20,R16 //IF R20> R16 (It's dim)
BRGE check

greateq:
LDI R21,0X01 //Here we are checking if it is greater than or equal to one (2^8 on or more)
CP R17,R21
brge light
CP R21, R17
brge dim

check: // The ADCL has a decimal value lower than 244 so we need either 2^9 or both 2^8 and 2^9 but ( 2^8 alone is not enough )
LDI R23,0x02 // Greater than or equal to 2 aka either (2^9 is on) or (2^8 and 2^9 together are on)
CP R17,R23
brge light
CP R23, R17
brge dim

light:
CBI PORTB, 5 // Turns off the LED

dim:
SBI PORTB, 5 // Turns on the LED

RJMP READ_ADC


The problem is that the LED always stays on.
The code is supposed to keep on checking whether the light intensity has exceeded 500 or not and if it did it should clear the bit which the LED is connected to , but if there is a light intensity more than 500 it should set the bit to 1 to light up the LED.
What is messing up the values in my code?










share|improve this question















sbi DDRB, 5 //led output
cbi DDRC, 0 //LDR input
sbi PORTC, 0 //pull up resistor

//---- Conversion starts here ----//
LDI R16 , 0x87
STS ADCSRA, R16
LDI R16 , 0x11

STS ADMUX, R16
//configurations done

READ_ADC:
//CBI PORTB,5
LDS R18, ADCSRA
SBR R18,0b01000000
STS ADCSRA,R18

KEEP_POLING :
LDS R19, ADCSRA
SBRS R19, 4
//
RJMP KEEP_POLING // loop bc conversion not complegted ADIGF =0+


LDS R16, ADCSRA
SBR R16,0b00010000
STS ADCSRA,R16

// above is setting bit 4 to 1 indicating that conversion over

//below is reading the high and low byte of the ADC
LDS R16,ADCL //8 bits
LDS R17,ADCH // read 2 bits


LDI R20, 0Xf4
CP R16,R20 //IF R16>=R20
BRGE greateq // The low bits are greater than 244 so if ADCH has a value of one which is 2^8=256+244=500
CP R20,R16 //IF R20> R16 (It's dim)
BRGE check

greateq:
LDI R21,0X01 //Here we are checking if it is greater than or equal to one (2^8 on or more)
CP R17,R21
brge light
CP R21, R17
brge dim

check: // The ADCL has a decimal value lower than 244 so we need either 2^9 or both 2^8 and 2^9 but ( 2^8 alone is not enough )
LDI R23,0x02 // Greater than or equal to 2 aka either (2^9 is on) or (2^8 and 2^9 together are on)
CP R17,R23
brge light
CP R23, R17
brge dim

light:
CBI PORTB, 5 // Turns off the LED

dim:
SBI PORTB, 5 // Turns on the LED

RJMP READ_ADC


The problem is that the LED always stays on.
The code is supposed to keep on checking whether the light intensity has exceeded 500 or not and if it did it should clear the bit which the LED is connected to , but if there is a light intensity more than 500 it should set the bit to 1 to light up the LED.
What is messing up the values in my code?







assembly arduino embedded adc






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 1:23

























asked Nov 11 at 1:17









Rana

62




62








  • 1




    These kind of problems are easier understood when a schematic is also attached. Should we assume the circuit is correct? You are driving the LED directly from the ATMega256p output pin and sensing the photo-resistor by ADC'verting the drop on it when put in a voltage divider, right? The cbi portb, 5 falls through to sbi portb, 5 that will undo the clear. Are you missing a rjmp portb, 5 before the label dim?
    – Margaret Bloom
    Nov 11 at 10:58














  • 1




    These kind of problems are easier understood when a schematic is also attached. Should we assume the circuit is correct? You are driving the LED directly from the ATMega256p output pin and sensing the photo-resistor by ADC'verting the drop on it when put in a voltage divider, right? The cbi portb, 5 falls through to sbi portb, 5 that will undo the clear. Are you missing a rjmp portb, 5 before the label dim?
    – Margaret Bloom
    Nov 11 at 10:58








1




1




These kind of problems are easier understood when a schematic is also attached. Should we assume the circuit is correct? You are driving the LED directly from the ATMega256p output pin and sensing the photo-resistor by ADC'verting the drop on it when put in a voltage divider, right? The cbi portb, 5 falls through to sbi portb, 5 that will undo the clear. Are you missing a rjmp portb, 5 before the label dim?
– Margaret Bloom
Nov 11 at 10:58




These kind of problems are easier understood when a schematic is also attached. Should we assume the circuit is correct? You are driving the LED directly from the ATMega256p output pin and sensing the photo-resistor by ADC'verting the drop on it when put in a voltage divider, right? The cbi portb, 5 falls through to sbi portb, 5 that will undo the clear. Are you missing a rjmp portb, 5 before the label dim?
– Margaret Bloom
Nov 11 at 10:58

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245010%2farduino-photo-resistor-lights-the-led-when-dark-dims-the-led-when-there-is-l%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53245010%2farduino-photo-resistor-lights-the-led-when-dark-dims-the-led-when-there-is-l%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Danny Elfman

Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues