#include <Servo.h>
 
Servo disk;
 
int diskpin = 3;
int diskstop = 50, diskmin = 100, diskmax = 165;
 
int optpin = 8;
 
void setup()
{
  Serial.begin(9600);
  pinMode(optpin, INPUT);
  digitalWrite(optpin, HIGH);
 
  disk.attach(diskpin);
  delay(2000);
  disk.write(diskstop);
  delay(3000);
}
 
int cur_time = 0;
int edge_ones = 0;
char last_opt = 0;
 
void loop()
{
  char optstatus = digitalRead(optpin);
  if (optstatus == 1 && last_opt == 0)
    edge_ones++;
  last_opt = optstatus;
 
  int new_time = millis()/1000;
  if (new_time != cur_time) {
    cur_time = new_time;
    Serial.print(edge_ones * 60, DEC);
    Serial.print(" RPM  ");
    edge_ones = 0;
 
    Serial.println(optstatus, DEC);
 
    if (Serial.available()) {
      char buf[8] = "";
      Serial.readBytesUntil('\n', buf, sizeof(buf));
      if (buf[0]) {
        int num = atoi(buf);
        Serial.println(num, DEC);
        if (num == 0)
          disk.write(diskstop);
        else if (1 <= num && num <= 100)
          disk.write(map(num, 1, 100, diskmin, diskmax));
      }
    }
  }
}
 
Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki