Page 197 - 15 Dangerously Mad Projects for the Evil Genius
P. 197

174      15 Dangerously Mad Projects for the Evil Genius



          LISTING 13-1    (continued)

              powerCount += power;
              oldPosition = position;


              // clip
              if (power > maxPower) power = maxPower;
              if (power < 0) power = 0;


              checkSerial();
              if (monitoring)
              {
                Serial.print(position);  Serial.print(",");
                Serial.println(velocity);
              }

              analogWrite(coilPin, power * objectPresent);
              delayMicroseconds(D);
           }

           int readAmbient()
           {
              digitalWrite(irPin, LOW);
               // allow time for LED and phototransistor to settle
              delayMicroseconds(100);
              int ambient = 1024 - analogRead(sensorPin);
              digitalWrite(irPin, HIGH);
              return ambient;
           }

           void checkSerial()
           {
              if (Serial.available())
              {
                char ch = Serial.read();
                if (ch == 'm')
                {
                   monitoring = ! monitoring;
                }
                if (ch == 'B')
                {
                   B += 5;
                   Serial.println(B);
                }
                if (ch == 'b')
                {
                   B -= 5;
                   Serial.println(B);
                }
              }
           }
   192   193   194   195   196   197   198   199   200   201   202