User Tools

Site Tools


project:biolab:centrifuge-sketch

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
project:biolab:centrifuge-sketch [2012/05/12 00:45] paskyproject:biolab:centrifuge-sketch [2014/10/11 15:01] (current) – button-controlled centrifuge pasky
Line 1: Line 1:
 +Centrifuge controlled over serial USB:
  
 +<code lang="C">
 +#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));
 +      }
 +    }
 +  }
 +}
 +</code>
 +
 +Centrifuge controlled via button:
 +
 +<code lang="C">
 +#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;
 + }
 +}
 +</code>
project/biolab/centrifuge-sketch.txt · Last modified: 2014/10/11 15:01 by pasky