Postat: 30 juni 2007, 18:33:57
Pennan bara magnetisk upptill?
Svenskt forum för elektroniksnack.
https://elektronikforumet.com/forum/
Kod: Markera allt
/*
* Pin 0 = Sensor
* Pin 1 =
* Pin 2 = PrimärSpole
* Pin 3 = PrimärSpole
* Pin 4 = SekundärSpole
* Pin 5 = SekundärSpole
* Pin 6 = DragSpole
* Pin 7 = DragSpole
* Pin 8
* Pin 9 = SetButtonPin?
* Pin 10 = LED1
* Pin 11 = LED2
*/
#include "WProgram.h"
double readHeight(int speedToSample);
void balance(int direction, int power);
const int off = 2, up=1, down=0, none=2, high=1, low=0;
double holdHeight=190.0, smallHeightChange=0.3,largeHeightChange=1.0;//190.0 in dark room
double lowTolerence=0.5,highTolerence=5.0,triggerBalance=4.0;
int count=0,lowCountTolerence=90,highCountTolerence=160,countOut=580;
int setButtonPin=9;
int setButton=LOW;
long startTime=millis();
long currentTime=millis();
const int quick=4, detailed=80;
double sensorReading=0.0;
double pencilDetection=1.5; //Har ingen aning om vad det ska vara utan att testa men funkar
//så länge.
int sensorPin = 0;
int primaryCoilPins[2]={
2,3};
int secondaryCoilPins[2]={
4,5};
int draggingCoilPins[2]={
6,7};
int ledPin[2]={
10,11};
void setup()
{
//Sätter de pinnar som används till spolarna till output och höga
for(int i=0;i<2;i++)
{
pinMode(primaryCoilPins[i],OUTPUT);
digitalWrite(primaryCoilPins[i],HIGH);
pinMode(secondaryCoilPins[i],OUTPUT);
digitalWrite(secondaryCoilPins[i],HIGH);
pinMode(draggingCoilPins[i], OUTPUT);
digitalWrite(draggingCoilPins[i], HIGH);
pinMode(ledPin[i], OUTPUT);
}
}
void loop()
{
sensorReading = analogRead(sensorPin)+0.0;
currentTime=millis();
// Om sensorn märker att någonting finns under den ska den böja reglera
if(sensorReading >= pencilDetection) /**********/
{
digitalWrite(draggingCoilPins[0], LOW);
digitalWrite(draggingCoilPins[1], LOW);
if(currentTime >= startTime + 200)
{
if (abs(count) < countOut)
{
holdHeight-=smallHeightChange/2;
if(count < -lowCountTolerence)
{
holdHeight+=smallHeightChange;
digitalWrite(ledPin[1],HIGH);
}
else if(count < -highCountTolerence)
{
holdHeight+=largeHeightChange;
digitalWrite(ledPin[1],HIGH);
}
else if(count > lowCountTolerence)
{
holdHeight-=smallHeightChange;
digitalWrite(ledPin[0],HIGH);
}
else if(count > highCountTolerence)
{
holdHeight-=largeHeightChange;
digitalWrite(ledPin[0],HIGH);
}
}
count=0;
startTime=millis();
}
if(sensorReading < holdHeight-highTolerence+triggerBalance)
{
balance(up,high);
}
else if (sensorReading < holdHeight-lowTolerence)
{
balance(up,low);
}
else if(sensorReading > holdHeight+highTolerence)
{
balance(down,high);
}
else if (sensorReading > holdHeight+lowTolerence+triggerBalance)
{
balance(down,low);
}
else
{
balance(off,low);
}
} /***************/
/*balance(off,low);
digitalWrite(ledPin[2],LOW);
delay(200);
balance(up,high);
digitalWrite(ledPin[0],HIGH);
delay(400);
balance(down,high);
digitalWrite(ledPin[0],LOW);
digitalWrite(ledPin[2],HIGH);
delay(400);*/
}
double readHeight(int speedToSample)
{
int numSamples=speedToSample;
double avg=0.0;
for(int i=0;i<numSamples;i++)
avg += (analogRead(sensorPin)+0.0);
avg = avg/(numSamples+0.0);
return avg;
}
void balance(int direction, int power)
{
if(direction==up)
{
count-=1;
digitalWrite(primaryCoilPins[0],LOW);
digitalWrite(primaryCoilPins[1],HIGH);
if(power==high)
{
count-=2;
digitalWrite(secondaryCoilPins[0],LOW);
digitalWrite(secondaryCoilPins[1],HIGH);
}
else
{
digitalWrite(secondaryCoilPins[1],LOW);
digitalWrite(secondaryCoilPins[0],LOW);
}
}
else if(direction==down)
{
count+=1;
digitalWrite(primaryCoilPins[0],HIGH);
digitalWrite(primaryCoilPins[1],LOW);
if(power==high)
{
count+=2;
digitalWrite(secondaryCoilPins[0],HIGH);
digitalWrite(secondaryCoilPins[1],LOW);
}
else
{
digitalWrite(secondaryCoilPins[1],LOW);
digitalWrite(secondaryCoilPins[0],LOW);
}
}
else
{
digitalWrite(primaryCoilPins[0],LOW);
digitalWrite(primaryCoilPins[1],LOW);
digitalWrite(secondaryCoilPins[0],LOW);
digitalWrite(secondaryCoilPins[1],LOW);
}
}