Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : relay control and led without delay?
Author | Message | ||||
LG046 Newbie Joined: 02/08/2012 Location: NetherlandsPosts: 12 |
hi i have 2 relays connected to my duinomite mega 1 controls a machine and the other is on a LED so when the machine is running LED is on and when machine is off led is off i have 1 digital output conntected to a external relay that will close or make up on if the machine needs to run or not only problem i have right now is that when the led is blinking the machine relay control is also paused and i dont want this.. is that possible to do different? durint 100-200 line ---- 1 SetPin 13,9 2 SetPin 16,2 3 Pin(0) = 0 4 Font 2 5 SetPin 14,9 6 Pin(14) = 1 7 Pin(13) = 1 10 SetTick 10000, 300 14 Do 15 Pin(0) = 0 17 Pause 100 20 Pin(0) = 1 30 If Pin(16) = 0 Then Pin(13) = 0 Else Pin(13) = 1 31 Cls 32 If Pin(16) = 1 Then Print "machine off" 33 If Pin(16) = 0 Then Print "machine on" esle GoTo 42 40 If Pin(16) = 0 Then Pin(14) = 0 Else Pin(14) = 1 42 Pause 500 45 If Pin(16) = 1 Then GoTo 100 Else GoTo 90 90 Loop Until Pin(0) = 1 95 Pin(13) = 0 96 Pin(14) = 1 98 End 100 SetTick 20000, 225 110 Do 115 Pin(0) = 0 : Pin(14) = 1 116 Pause 2000 117 Pin(0) = 1 : Pin(14) = 0 120 If Pin(16) = 0 Then Pin(13) = 0 Else Pin(13) = 1 130 Cls 140 If Pin(16) = 1 Then Print "to fast OFF" 150 If Pin(16) = 0 Then Print "almost finisched " 160 Pause 2000 200 Loop Until Pin(0) = 1 210 Pin(13) = 1 220 End 225 settick 10000, 1000 230 Pin(13) = 1 240 Pin(0) = 1 245 Pin(14) = 1 250 Print " finisched " 255 Pause 3000 260 Cls 270 If Pin(0) = 1 Then GoTo 280 Else GoTo 230 280 end 300 Print "stopt by timer " 301 Pin(13) = 0 302 Pin(14) = 1 : Pause 500 : Pin(14) = 0 303 Pause 500 305 Cls 310 If Pin(0) = 1 Then GoTo 280 Else GoTo 300 -- i did set some timers low just for testing so disregard that. |
||||
centrex Guru Joined: 13/11/2011 Location: AustraliaPosts: 320 |
connect the led with a suitable dropping resistor across the relay both will operate at the same time. I hope this is what you meant. centrex Cliff |
||||
LG046 Newbie Joined: 02/08/2012 Location: NetherlandsPosts: 12 |
no i want the led to blink at its own speed independent from the other relay |
||||
ajkw Senior Member Joined: 29/06/2011 Location: AustraliaPosts: 290 |
you can only have 1 active SETTICK. In your code you have a SETTICK at line 100 which is immediately canceled at another SETTICK at line 225. It would be nice to have multiple timers (settick) but unfortunately you cannot. |
||||
LG046 Newbie Joined: 02/08/2012 Location: NetherlandsPosts: 12 |
i have a Do Loop for that first settick after first settick is timeover it go's to 100 not before because of the Do Loop or am i missing something |
||||
BobD Guru Joined: 07/12/2011 Location: AustraliaPosts: 935 |
You have a syntax error on line 33, should be ELSE not ESLE. Your use of SETTICK is not valid. It is an interrupt and as such it needs to point to a pseudo subroutine which has an IRETURN to end the routine. It also needs to point to a valid line number which makes your use on line 225 invalid as it is pointing to a non-existent line number. You probably won't find out about the invalid line number until that interrupt pops and has nowhere to go. Also, interrupts are disabled once an interrupt is taken and are not re-enabled until an IRETURN is processed. Your program will only interrupt once and won't return to the code where the interrupt happened. Because it is a timer interrupt it will happen when the time expires and will interrupt whatever the machine is doing at that instant. It won't change the state of any pins etc. unless you tell the interrupt routine to do this. If it interrupts a sequence of lines of code that is time dependent (such as writing to an external device) it may cause some problems as that time will be extended by the time to service the interrupt. While it is servicing the interrupt it will not be doing anything else. Follows are quotes from the MMBasic V3 manual (I don't have an earlier version). It refers to SETTICK This will setup a periodic interrupt (or “tick”). The time between interrupts is ‘period’ milliseconds and ‘target' is the line number or label of the interrupt routine. See also IRETURN to return from the interrupt. and about IRETURN Returns from an interrupt. The next statement to be executed will be the one that was about to be executed when the interrupt was detected. and here is some more on interrupts Using SETTICK you can setup a “tick” which will generate a regular interrupt with a period from one millisecond to over a month. Any external I/O pin can be configured to generate an interrupt using the SETPIN command with up to 21 interrupts (including the tick interrupt) active at any one time. Interrupts can be set up to occur on a rising or falling digital input signal and will cause an immediate branch to a specified line number or label (similar to a GOSUB). The target can be the same or different for each interrupt. Return from an interrupt is via the IRETURN statement. All statements (including GOSUB/RETURN) can be used within an interrupt. If two or more interrupts occur at the same time they will be processed in order of pin numbers (ie, an interrupt on pin 1 will have the highest priority). During processing of an interrupt all other interrupts are disabled until the interrupt routine returns with an IRETURN. During an interrupt (and at all times) the value of the interrupt pin can be accessed using the PIN() function. A periodic interrupt (or regular “tick”) with a period specified in milliseconds can be setup using the SETTICK statement. This interrupt has the lowest priority. Interrupts can occur at any time but they are disabled during INPUT statements. If you need to get input from the keyboard while still accepting interrupts you should use the INKEY$ function. When using interrupts the main program is completely unaffected by the interrupt activity unless a variable used by the main program is changed during the interrupt. For most programs MMBasic will respond to an interrupt in under 100µS. To prevent slowing the main program by too much an interrupt should be short and execute the IRETURN statement as soon as possible. Also remember to disable an interrupt when you have finished needing it – background interrupts can cause strange and non-intuitive bugs. Hope all this helps. |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6098 |
This is one way I get multiple timers from the one settick command. You have to be sure that the routines finish before the next interrupt. On my weather station The power led flashes one per second (I turn it on at the start of the interrupt and off again just before the IRETURN. That way I can use a CRO to time all the routines.) Wind gusts are recorded every 10 seconds, Temperatures etc recorded every 5 minutes. etc..... ticks=0
SETTICK 1000,_readAll 'one interrupt every second . . . . . _readAll: PIN(0)= 1 ' turn the power led on ticks=ticks+1 gosub _onesec if ticks mod 10=0 then gosub _tensecs if ticks mod 300=0 then gosub _fiveminutes . . . PIN(0)= 0 ' turn the power led off ireturn Jim VK7JH MMedit  MMBasic Help |
||||
Print this page |