Shop OBEX P1 Docs P2 Docs Learn Events
LED blinking?? — Parallax Forums

LED blinking??

marcomendozamarcomendoza Posts: 4
edited 2009-12-02 00:07 in Learn with BlocklyProp
I have made a "automatic night light" for class basically i am using a ds1302 , crystal and a photo resistor. also using the .01 capacitor and appropriate wires
this is what it does.
automatically turns on the led if the time is more than 45 seconds(46-59) if it less than 45 seconds it will turn on the LED if dark only.
i am having trouble when it is less than 45 seconds it will cause the LED to blink very fast instead of a solid on when it is dark.

here is my program i hope someone can help



' {$STAMP BS2}
' {$PBASIC 2.5}




'
[noparse][[/noparse] I/O Definitions ]

DataIO PIN 0 ' DS1302.6
Clock PIN 1 ' DS1302.7
CS1302 PIN 2 ' DS1302.5
LED PIN 15

'
[noparse][[/noparse] Constants ]

WrSecs CON $80 ' Write Seconds
RdSecs CON $81 ' Read Seconds
WrMins CON $82 ' Write Minutes
RdMins CON $83 ' Read Minutes
WrHrs CON $84 ' Write Hours
RdHrs CON $85 ' Read Hours
CWPr CON $8E ' Write Protect Register
WPr1 CON $80 ' Set Write Protect
WPr0 CON $00 ' Clear Write Protect
WrBurst CON $BE ' Write Burst Of Data
RdBurst CON $BF ' Read Burst Of Data
WrRam CON $C0 ' Write RAM Data
RdRam CON $C1 ' Read RAM Data
Hr24 CON 0 ' 24 Hour Mode
Hr12 CON 1 ' 12 Hour Mode
Dark CON 150

'
[noparse][[/noparse] Variables ]

index VAR Byte ' Loop Counter
reg VAR Byte ' Read/Write Address
ioByte VAR Byte ' Data To/From DS1302
secs VAR Byte ' Seconds
secs01 VAR secs.LOWNIB
secs10 VAR secs.HIGHNIB
mins VAR Byte ' Minutes
mins01 VAR mins.LOWNIB
mins10 VAR mins.HIGHNIB
hrs VAR Byte ' Hours
hrs01 VAR hrs.LOWNIB
hrs10 VAR hrs.HIGHNIB
date VAR Byte
month VAR Byte
day VAR Nib ' Day
year VAR Byte ' Year

ampm VAR hrs.BIT5 ' AM/PM Flag Bit
clockMode VAR hrs.BIT7 ' 12/24 Hour Mode Bit
ampmFlag VAR Bit ' 0 = AM, 1 = PM
modeFlag VAR Bit ' 0 = 24, 1 = 12 (Hours)

work VAR Byte ' Work Data
time VAR Word

'
[noparse][[/noparse] EEPROM Data ]

Sun DATA "SUN", 0 ' Day Abbreviations
Mon DATA "MON", 0
Tue DATA "TUE", 0 ' These data statements could
Wed DATA "WED", 0 ' contain the full day name and
Thu DATA "THU", 0 ' the code would still work
Fri DATA "FRI", 0 ' without change.
Sat DATA "SAT", 0


'
[noparse][[/noparse] Initialization ]

Init:
reg = CWPr ' Initialize DS1302
ioByte = WPr0 ' Clear Write Protect
GOSUB RTC_Out ' Send Command

'
[noparse][[/noparse] Program Code ]

Start:
GOSUB Get_Time ' Get The Current Date/Time
GOSUB Show_Time ' Display It
DEBUG HOME, secs
IF secs > $45 THEN
HIGH LED
ELSE
LOW LED
ENDIF
GOSUB Get_light

'
[noparse][[/noparse] Subroutines ]
Get_light:
HIGH 7 ' Read photoresistor with RCTIME
RCTIME 7, 1, time
DEBUG HOME, "time = ", DEC4 time ' Print value to Debug Terminal
IF (time > Dark ) THEN ' Compare reading to known dark value
HIGH 15
ELSE
LOW 15
ENDIF
GOTO Start

Show_Time:
' This routine uses DEBUG to display the Date/Time information but you
' would customize the routine to display the data you want on the device
' you want. Not all information needs to be displayed.

LOOKUP (day - 1), [noparse][[/noparse]Sun, Mon, Tue, Wed, Thu, Fri, Sat], work

DO ' Display Day Abbreviation
READ work, ioByte ' Read Each Character Into ioByte
IF (ioByte = 0) THEN EXIT ' If 0 then done
DEBUG ioByte ' Send Character To DEBUG Screen
work = work + 1 ' Increment Address
LOOP ' Next Character, If 0 Then Done

modeFlag = clockMode ' Save 12/24 Hour Clock Mode
clockMode = 0 ' Clear BIT7
ampmFlag = ampm ' Save AM/PM Flag

IF modeFlag = 1 THEN ampm = 0 ' Clear BIT5 (12 Hour Mode only!)

DEBUG " ", HEX2 month, "/", HEX2 date, "/", HEX2 year, " "

IF (modeFlag = Hr24) THEN ' Check For 24 Hour Display mode
DEBUG HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
ELSE
DEBUG HEX2 hrs, ":", HEX2 mins ' Print Hours/Minutes
IF ampmFlag = 0 THEN
DEBUG "AM"
ELSE
DEBUG "PM"
ENDIF

DEBUG " [noparse][[/noparse]", HEX2 secs, "]"
ENDIF
RETURN

RTC_Out:
HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]reg, ioByte]
LOW CS1302 ' Deselect DS1302
RETURN





Get_Time: ' DS1302 Burst Read
HIGH CS1302 ' Select DS1302
SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]RdBurst]
SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]secs, mins, hrs, date, month, day, year]
LOW CS1302 ' Deselect DS1302
RETURN

Comments

  • FranklinFranklin Posts: 4,747
    edited 2009-12-01 18:51
    Make sure your subroutine "get light" is doing what you want (I think not) Also if you assign a name to a pin (LED pin 15) you should use that throughout your code rather than using the name in places and the pin number in others. Just good coding.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    - Stephen
  • marcomendozamarcomendoza Posts: 4
    edited 2009-12-01 22:23
    ok i changed it to this and it seems like it is blinking faster. is it possible i can use a photo-resistor without a capacitor because i think that the capacitor is causing this due to charge and discharge times....
    i am using a .01 microfarad capacitor.....



    ' {$STAMP BS2}
    ' {$PBASIC 2.5}




    '
    [noparse][[/noparse] I/O Definitions ]

    DataIO PIN 0 ' DS1302.6
    Clock PIN 1 ' DS1302.7
    CS1302 PIN 2 ' DS1302.5
    LED PIN 15

    '
    [noparse][[/noparse] Constants ]

    WrSecs CON $80 ' Write Seconds
    RdSecs CON $81 ' Read Seconds
    WrMins CON $82 ' Write Minutes
    RdMins CON $83 ' Read Minutes
    WrHrs CON $84 ' Write Hours
    RdHrs CON $85 ' Read Hours
    CWPr CON $8E ' Write Protect Register
    WPr1 CON $80 ' Set Write Protect
    WPr0 CON $00 ' Clear Write Protect
    WrBurst CON $BE ' Write Burst Of Data
    RdBurst CON $BF ' Read Burst Of Data
    WrRam CON $C0 ' Write RAM Data
    RdRam CON $C1 ' Read RAM Data
    Hr24 CON 0 ' 24 Hour Mode
    Hr12 CON 1 ' 12 Hour Mode
    Dark CON 150

    '
    [noparse][[/noparse] Variables ]

    index VAR Byte ' Loop Counter
    reg VAR Byte ' Read/Write Address
    ioByte VAR Byte ' Data To/From DS1302
    secs VAR Byte ' Seconds
    secs01 VAR secs.LOWNIB
    secs10 VAR secs.HIGHNIB
    mins VAR Byte ' Minutes
    mins01 VAR mins.LOWNIB
    mins10 VAR mins.HIGHNIB
    hrs VAR Byte ' Hours
    hrs01 VAR hrs.LOWNIB
    hrs10 VAR hrs.HIGHNIB
    date VAR Byte
    month VAR Byte
    day VAR Nib ' Day
    year VAR Byte ' Year

    ampm VAR hrs.BIT5 ' AM/PM Flag Bit
    clockMode VAR hrs.BIT7 ' 12/24 Hour Mode Bit
    ampmFlag VAR Bit ' 0 = AM, 1 = PM
    modeFlag VAR Bit ' 0 = 24, 1 = 12 (Hours)

    work VAR Byte ' Work Data
    time VAR Word

    '
    [noparse][[/noparse] EEPROM Data ]

    Sun DATA "SUN", 0 ' Day Abbreviations
    Mon DATA "MON", 0
    Tue DATA "TUE", 0 ' These data statements could
    Wed DATA "WED", 0 ' contain the full day name and
    Thu DATA "THU", 0 ' the code would still work
    Fri DATA "FRI", 0 ' without change.
    Sat DATA "SAT", 0


    '
    [noparse][[/noparse] Initialization ]

    Init:
    reg = CWPr ' Initialize DS1302
    ioByte = WPr0 ' Clear Write Protect
    GOSUB RTC_Out ' Send Command

    '
    [noparse][[/noparse] Program Code ]

    Start:
    DO
    GOSUB Get_Time ' Get The Current Date/Time
    GOSUB Show_Time ' Display It
    GOSUB Get_light
    IF secs > $45 THEN
    HIGH LED
    ELSE
    LOW LED
    ENDIF
    LOOP

    '
    [noparse][[/noparse] Subroutines ]
    Get_light:
    HIGH 7 ' Read photoresistor with RCTIME
    RCTIME 7, 1, time
    DEBUG HOME, "time = ", DEC4 time ' Print value to Debug Terminal
    IF secs < $46 AND time > Dark THEN ' Compare reading to known dark value
    HIGH LED
    ELSE
    LOW LED
    ENDIF
    RETURN

    Show_Time:
    ' This routine uses DEBUG to display the Date/Time information but you
    ' would customize the routine to display the data you want on the device
    ' you want. Not all information needs to be displayed.

    LOOKUP (day - 1), [noparse][[/noparse]Sun, Mon, Tue, Wed, Thu, Fri, Sat], work

    DO ' Display Day Abbreviation
    READ work, ioByte ' Read Each Character Into ioByte
    IF (ioByte = 0) THEN EXIT ' If 0 then done
    DEBUG ioByte ' Send Character To DEBUG Screen
    work = work + 1 ' Increment Address
    LOOP ' Next Character, If 0 Then Done

    modeFlag = clockMode ' Save 12/24 Hour Clock Mode
    clockMode = 0 ' Clear BIT7
    ampmFlag = ampm ' Save AM/PM Flag

    IF modeFlag = 1 THEN ampm = 0 ' Clear BIT5 (12 Hour Mode only!)

    DEBUG " ", HEX2 month, "/", HEX2 date, "/", HEX2 year, " "

    IF (modeFlag = Hr24) THEN ' Check For 24 Hour Display mode
    DEBUG HEX2 hrs, ":", HEX2 mins, ":", HEX2 secs
    ELSE
    DEBUG HEX2 hrs, ":", HEX2 mins ' Print Hours/Minutes
    IF ampmFlag = 0 THEN
    DEBUG "AM"
    ELSE
    DEBUG "PM"
    ENDIF

    DEBUG " [noparse][[/noparse]", HEX2 secs, "]"
    ENDIF
    RETURN

    RTC_Out:
    HIGH CS1302 ' Select DS1302
    SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]reg, ioByte]
    LOW CS1302 ' Deselect DS1302
    RETURN


    Get_Time: ' DS1302 Burst Read
    HIGH CS1302 ' Select DS1302
    SHIFTOUT DataIO, Clock, LSBFIRST, [noparse][[/noparse]RdBurst]
    SHIFTIN DataIO, Clock, LSBPRE, [noparse][[/noparse]secs, mins, hrs, date, month, day, year]
    LOW CS1302 ' Deselect DS1302
    RETURN
  • marcomendozamarcomendoza Posts: 4
    edited 2009-12-01 22:25
    then again it runs fine as this with no blinking as a solid light.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
    LED PIN 15
    Dark CON 190
    time VAR Word
    DO
    HIGH 7 ' Read photoresistor with RCTIME
    PAUSE 100
    RCTIME 7, 1, time
    DEBUG HOME, "time = ", DEC5 time ' Print value to Debug Terminal
    IF (time > Dark) THEN ' Compare reading to known dark value
    HIGH LED
    ELSE
    LOW LED
    ENDIF
    LOOP
  • marcomendozamarcomendoza Posts: 4
    edited 2009-12-02 00:07
    wow nevermind!!! i finally figured it out after hours of thinking and experimenting...

    i had 2 many high LED commands !!! i simply fixed this with a goto command instead of high LED
    and my goto was to the high LED!!!!


    yay!!!!
Sign In or Register to comment.