lotus.ino 2.53 KB
Newer Older
Daniel Stan's avatar
Daniel Stan committed
1 2

// Pin du moteur 
Daniel Stan's avatar
Daniel Stan committed
3
#define RF_M_CONT 12 //EN 3,4
Daniel Stan's avatar
Daniel Stan committed
4
#define RF_M_DRV1 11 //3A
Daniel Stan's avatar
Daniel Stan committed
5
#define RF_M_DRV2 10 //4A
Daniel Stan's avatar
Daniel Stan committed
6

7 8 9 10
// Pin relai ampoule
#define PIN_BULB 9

// Pin pour mesurer la position
Daniel Stan's avatar
Daniel Stan committed
11 12
#define PIN_POS A0

Daniel Stan's avatar
Daniel Stan committed
13
// Position hautes et basses
14 15
#define POS_HIGH 275
#define POS_LOW 105
Daniel Stan's avatar
Daniel Stan committed
16
#define BOUNCE 3
Daniel Stan's avatar
Daniel Stan committed
17

Daniel Stan's avatar
Daniel Stan committed
18 19
void setup()
{
Daniel Stan's avatar
Daniel Stan committed
20 21
  // Default light
  pinMode(PIN_BULB, OUTPUT);
Daniel Stan's avatar
Daniel Stan committed
22
  digitalWrite(PIN_BULB, HIGH);
Daniel Stan's avatar
Daniel Stan committed
23 24 25 26 27 28 29 30 31 32 33
   
  // Serial !
  Serial.begin(9600);

  // And the motor
  pinMode(RF_M_CONT, OUTPUT);
  pinMode(RF_M_DRV1, OUTPUT);
  pinMode(RF_M_DRV2, OUTPUT);
  digitalWrite(RF_M_CONT, LOW);
  digitalWrite(RF_M_DRV1, LOW);
  digitalWrite(RF_M_DRV2, LOW);
Daniel Stan's avatar
Daniel Stan committed
34 35
}

Daniel Stan's avatar
Daniel Stan committed
36
void move(bool going_up, int target=0, int timeout=8000) {
Daniel Stan's avatar
Daniel Stan committed
37 38
  int comp;
  int val;
Daniel Stan's avatar
Daniel Stan committed
39 40 41 42 43 44
  unsigned long deadline;

  deadline = millis() + timeout;

  if(target)
  {
Daniel Stan's avatar
Daniel Stan committed
45
      target = min(max(target, POS_LOW), POS_HIGH);
46
      going_up = get_position() < target;
Daniel Stan's avatar
Daniel Stan committed
47
  }
Daniel Stan's avatar
Daniel Stan committed
48 49 50 51 52
  else
  {
      target = going_up?POS_HIGH:POS_LOW;
  }

Daniel Stan's avatar
Daniel Stan committed
53 54 55 56 57 58
  Serial.print("Move:");
  Serial.print(going_up?"Up":"Down");
  Serial.print(",Target:");
  Serial.print(target);
  Serial.print(",Timeout:");
  Serial.println(timeout);
Daniel Stan's avatar
Daniel Stan committed
59

Daniel Stan's avatar
Daniel Stan committed
60 61 62 63 64 65
  if(going_up) {
    comp=1;
    digitalWrite(RF_M_DRV1, HIGH);
    digitalWrite(RF_M_DRV2, LOW);    
  } else {
    comp=-1;
Daniel Stan's avatar
Daniel Stan committed
66
    digitalWrite(RF_M_DRV1, LOW);
Daniel Stan's avatar
Daniel Stan committed
67 68 69
    digitalWrite(RF_M_DRV2, HIGH);    
  }
  digitalWrite(RF_M_CONT, HIGH);
Daniel Stan's avatar
Daniel Stan committed
70 71 72 73
  
  do {
    delay(1);
    val = get_position();
Daniel Stan's avatar
Daniel Stan committed
74
  }
Daniel Stan's avatar
Daniel Stan committed
75 76 77 78 79 80
  while(val *comp < target*comp && millis() < deadline);

  // End of move
  Serial.print("Done:");
  report_pos();

Daniel Stan's avatar
Daniel Stan committed
81 82 83
  digitalWrite(RF_M_CONT, LOW);
  digitalWrite(RF_M_DRV1, LOW);
  digitalWrite(RF_M_DRV2, LOW);
Daniel Stan's avatar
Daniel Stan committed
84
}
Daniel Stan's avatar
Daniel Stan committed
85

Daniel Stan's avatar
Daniel Stan committed
86 87 88 89 90 91
int get_position() {
  int value = 0;
  const int count = 10;
  for(int i=0; i < count; i++)
    value += analogRead(PIN_POS);
  return value/count;
Daniel Stan's avatar
Daniel Stan committed
92 93
}

Daniel Stan's avatar
Daniel Stan committed
94 95 96
void report_pos() {
  Serial.print("Report:"); Serial.println(get_position());
}
Daniel Stan's avatar
Daniel Stan committed
97 98 99

void loop() {
  int incomingByte = 0;
Daniel Stan's avatar
Daniel Stan committed
100 101
  if(Serial.available()) {
    incomingByte = Serial.read();
Daniel Stan's avatar
Daniel Stan committed
102 103 104 105 106 107 108 109 110 111
    // Debug:
    Serial.print("Input:"); Serial.println(incomingByte);
    switch(incomingByte) {
      case 'd':
        move(false, 0, 300);
        break;
      case 'u':
        move(true, 0, 500);
        break;
      case 'U':
112
        move(true, 1024);
Daniel Stan's avatar
Daniel Stan committed
113 114
        break;
      case 'D':
115
        move(false, 1);
Daniel Stan's avatar
Daniel Stan committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
        break;
      case 'm':
        move(false, Serial.parseInt());
      case 'r':
        report_pos();
        break;
      case 'o':
        digitalWrite(PIN_BULB, LOW);
        break;
      case 'f':
        digitalWrite(PIN_BULB, HIGH);
        break;
      default:
        Serial.println("Ignored");
    }
Daniel Stan's avatar
Daniel Stan committed
131
  }
Daniel Stan's avatar
Daniel Stan committed
132 133 134
}