Statistics
| Revision:

root / Ports / examples / opto_demo / opto_demo.pde

History | View | Annotate | Download (1.8 KB)

1
//>>> The latest version of this code can be found at https://github.com/jcw/ !!
2
3
// Demo of the opto-coupler plug
4
// 2010-09-26 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
5
// $Id: opto_demo.pde 7763 2011-12-11 01:28:16Z jcw $
6
7
#include <Ports.h>
8
#include <RF12.h>
9
10
Port optoIn (3), optoOut (4), testIn (2), testOut (1);
11
byte state; // counts on each cycle, bits 3..0 used as test signals
12
13
// has to be defined because we're using the watchdog for low-power waiting
14
ISR(WDT_vect) { Sleepy::watchdogEvent(); }
15
16
void setup () {
17
    rf12_initialize(9, RF12_868MHZ, 5);
18
    
19
    // connect to opto-coupler plug as inputs with pull-ups enabled 
20
    optoIn.digiWrite(1);
21
    optoIn.mode(INPUT);
22
    optoIn.digiWrite2(1);
23
    optoIn.mode2(INPUT);
24
    // connect to opto-coupler plug as output 
25
    optoOut.mode(OUTPUT);
26
    optoOut.mode2(OUTPUT);
27
    // the testIn port will be used to generate test signals to optoIn
28
    testIn.mode(OUTPUT);
29
    testIn.mode2(OUTPUT);
30
    // the testOut port will be used to verify test signals from optoOut
31
    testOut.digiWrite(1);   // pull-up
32
    testOut.mode(INPUT);
33
    testOut.digiWrite2(1);  // pull-up
34
    testOut.mode2(INPUT);
35
}
36
37
void loop () {
38
    ++state;
39
40
    testIn.digiWrite(bitRead(state, 0));
41
    testIn.digiWrite2(bitRead(state, 1));
42
    optoOut.digiWrite(bitRead(state, 2));
43
    optoOut.digiWrite2(bitRead(state, 3));
44
    delay(2);
45
46
    byte payload[8];
47
    payload[0] = bitRead(state, 0);
48
    payload[1] = bitRead(state, 1);
49
    payload[2] = bitRead(state, 2);
50
    payload[3] = bitRead(state, 3);
51
    payload[4] = optoIn.digiRead();
52
    payload[5] = optoIn.digiRead2();
53
    payload[6] = testOut.digiRead();
54
    payload[7] = testOut.digiRead2();
55
    
56
    while (!rf12_canSend())
57
        rf12_recvDone();
58
    rf12_sendStart(0, payload, sizeof payload, 2);
59
60
    Sleepy::loseSomeTime(1000);
61
}