Code.c


1
ÿþ#include <SoftwareSerial.h>
2
#include "DualVNH5019MotorShield.h"
3
DualVNH5019MotorShield md;
4

5
int command = 0;  //incoming serial data
6
int led = 11;
7
int ledPin = 13;
8
int buttonpin = 5;
9
SoftwareSerial bluetooth(A2, A3);
10
int cmd;
11
#define SERVO_PIN 3
12
uint16_t volatile servoTime = 0;
13
uint16_t volatile servoHighTime = 3000;
14
boolean volatile servoHigh = false;
15

16

17
void setup() {
18
  bluetooth.begin(9600);
19
  Serial.begin(9600); // set up Serial library at 9600 bps - this is the speed the serial interface will work all
20
  pinMode(led, OUTPUT); //connect here the blue LED
21
  pinMode (ledPin, OUTPUT);
22
  pinMode (buttonpin, OUTPUT);
23
  md.init();
24
  servoInit();
25
  
26
}
27

28
// This ISR runs after Timer 2 reaches OCR2A and resets.
29
// In this ISR, we set OCR2A in order to schedule when the next
30
// interrupt will happen.
31
// Generally we will set OCR2A to 255 so that we have an
32
// interrupt every 128 us, but the first two interrupt intervals
33
// after the rising edge will be smaller so we can achieve
34
// the desired pulse width.
35
ISR(TIMER2_COMPA_vect)
36
{
37
  // The time that passed since the last interrupt is OCR2A + 1
38
  // because the timer value will equal OCR2A before going to 0.
39
  servoTime += OCR2A + 1;
40
   
41
  static uint16_t highTimeCopy = 3000;
42
  static uint8_t interruptCount = 0;
43
   
44
  if(servoHigh)
45
  {
46
    if(++interruptCount == 2)
47
    {
48
      OCR2A = 255;
49
    }
50
 
51
    // The servo pin is currently high.
52
    // Check to see if is time for a falling edge.
53
    // Note: We could == instead of >=.
54
    if(servoTime >= highTimeCopy)
55
    {
56
      // The pin has been high enough, so do a falling edge.
57
      digitalWrite(SERVO_PIN, LOW);
58
      servoHigh = false;
59
      interruptCount = 0;
60
    }
61
  } 
62
  else
63
  {
64
    // The servo pin is currently low.
65
     
66
    if(servoTime >= 40000)
67
    {
68
      // We've hit the end of the period (20 ms),
69
      // so do a rising edge.
70
      highTimeCopy = servoHighTime;
71
      digitalWrite(SERVO_PIN, HIGH);
72
      servoHigh = true;
73
      servoTime = 0;
74
      interruptCount = 0;
75
      OCR2A = ((highTimeCopy % 256) + 256)/2 - 1;
76
    }
77
  }
78
}
79
 
80
void servoInit()
81
{
82
  digitalWrite(SERVO_PIN, LOW);
83
  pinMode(SERVO_PIN, OUTPUT);
84
   
85
  // Turn on CTC mode.  Timer 2 will count up to OCR2A, then
86
  // reset to 0 and cause an interrupt.
87
  TCCR2A = (1 << WGM21);
88
  // Set a 1:8 prescaler.  This gives us 0.5us resolution.
89
  TCCR2B = (1 << CS21);
90
   
91
  // Put the timer in a good default state.
92
  TCNT2 = 0;
93
  OCR2A = 255;
94
   
95
  TIMSK2 |= (1 << OCIE2A);  // Enable timer compare interrupt.
96
  sei();   // Enable interrupts.
97
}
98
 
99
void servoSetPosition(uint16_t highTimeMicroseconds)
100
{
101
  TIMSK2 &= ~(1 << OCIE2A); // disable timer compare interrupt
102
  servoHighTime = highTimeMicroseconds * 2;
103
  TIMSK2 |= (1 << OCIE2A); // enable timer compare interrupt
104
}
105

106

107
void loop() {
108
 
109
  if (bluetooth.available()) {
110
    cmd = bluetooth.read();
111
    Serial.println(cmd);
112
    Serial.write(cmd);
113
    
114
    switch (cmd)
115
    {