//by stephan schulz //used for project 'exercise machine' //see it at http://maybevideodoes.de/sites/neon.html int mode = 2; //or 1 but it is not ampliyied enough int pulse = 100; // the wait time between high and low for one step sequence int pulseDir = 50; int on = 1; int off = 0; int aa [] ={ 0,0,0,0,0}; int lastSensor[] = { 0,0,0,0,0}; //-----------motor int stepCount[] = { 0,0,0,0,0}; int sensor[] = { 0,0,0,0,0}; int dirM[] = { 0,0,0,0,0}; signed int diffM[] = { 0,0,0,0,0}; void setup() { Serial.begin(9600); Serial.println("---start---"); int i; for (i=0; i <= 9; i++) { pinMode(i, OUTPUT); } for (i=0; i <= 4; i++) { lastSensor[i] = analogRead(i) * mode; // remembers poti pos for comparison and noise reduction stepCount[i] = checkSensor(i); // prevents motors from turning on start intUnit(i); } } void loop() { for (int i=0; i <= 4; i++) { if (abs(sensor[i]-stepCount[i]) == 0) { intUnit(i); } else { stepCount[i] = stepCount[i] + dirM[i]; aa[i] = on; } } stepIt(); } int intUnit(int mUnit) { aa[mUnit] = off; sensor[mUnit] = checkSensor(mUnit); diffM[mUnit] = sensor[mUnit]-stepCount[mUnit]; signed int diff = diffM[mUnit]; if (diff != 0) { dirM[mUnit] = diff / abs(diff); int dir = abs( (dirM[mUnit] - 1) / 2); digitalWrite((mUnit*2)+1, dir); // delayMicroseconds(pulseDir); } } int checkSensor(int resPin) { int temp_bend = analogRead(resPin) * mode; int oldSensor = lastSensor[resPin]; int dd = abs(temp_bend - oldSensor); if (dd > 15*mode) { lastSensor[resPin] = temp_bend; temp_bend = (temp_bend/4); } else { temp_bend = (oldSensor/4); } return round(temp_bend); // 2.2 seems to be the convertion from sensor value to motor value, //use /4for half stepping, /2 for quarter stepping } void stepIt() { delayMicroseconds(pulse); digitalWrite(0, LOW); digitalWrite(2, LOW); digitalWrite(4, LOW); digitalWrite(6, LOW); digitalWrite(8, LOW); // delayMicroseconds(pulse); // 1000 micro = 1 milli delay(3); digitalWrite(0, aa[0]); digitalWrite(2, aa[1]); digitalWrite(4, aa[2]); digitalWrite(6, aa[3]); digitalWrite(8, aa[4]); // delayMicroseconds(pulse); delay(3); }