Centrifuge controlled over serial USB:

#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));
      }
    }
  }
}

Centrifuge controlled via button:

#include <Servo.h>
 
Servo disk;
 
int diskpin = 8;
int diskstop = 50, diskmin = 100, diskmax = 165;
 
int optpin = 3;
int btnpin = 12;
int ledpin0 = 13, ledpin1 = 11;
 
void setup()
{
	Serial.begin(9600);
	pinMode(optpin, INPUT);
	digitalWrite(optpin, HIGH);
 
	pinMode(btnpin, INPUT);
	digitalWrite(btnpin, HIGH);
 
	pinMode(ledpin0, OUTPUT);
	digitalWrite(ledpin0, HIGH);
	pinMode(ledpin1, OUTPUT);
	digitalWrite(ledpin1, HIGH);
 
#if 0
	disk.attach(diskpin);
	delay(2000);
	disk.write(diskstop);
	delay(3000);
#else
	disk.attach(diskpin);
	disk.write(diskstop);
	delay(4000);
#endif
 
	digitalWrite(ledpin0, LOW);
	digitalWrite(ledpin1, LOW);
}
 
int cur_time = 0;
int edge_ones = 0;
char last_opt = 0;
int status = 0; // 0: off, 1: on
 
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);
	}
 
	char btnstatus = ! digitalRead(btnpin); // button press grounds the pin
	if (status == 0 && btnstatus == 1) {
		digitalWrite(ledpin0, HIGH);
		digitalWrite(ledpin1, HIGH);
		disk.write(diskmax);
		status = 1;
	} else if (status == 1 && btnstatus == 0) {
		disk.write(diskstop);
		digitalWrite(ledpin0, LOW);
		digitalWrite(ledpin1, LOW);
		status = 0;
	}
}
 
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