Men hur gjorde du?
Kopierade in i typ notepad bara då, eller?
Och formateringen/tabbar behålls då, eller?
Förlåt, jag är så otroligt dålig på datorer!
I synnerhet Microsoft/Windows

MVH/Roger
Kod: Markera allt
[code]
/*
Poll
*/
//int servo=13;
//int PotValue;
Kod: Markera allt
text
Kod: Markera allt
/*
Poll
*/
//int servo=13;
//int PotValue;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(13, OUTPUT); //output for Servo/debugging
pinMode(7, OUTPUT); //LED for memoryFull
// initialize the digital pin as an input.
// Pin 2&3 are connected to a button tied to GND with pullup 10K to VCC.
pinMode(2, INPUT); //input for NewUser button
pinMode(3, INPUT); //input for Cal button
}
int k; //will be globally calibrated
int temp;
int strengthArray[500]; //array for converted samples (RAM=2kB, 1 value=10bit~2B...PROBLEM!)
int reg1_stat;
int reg1_dyn;
int reg2_stat;
int reg2_dyn;
int reg3_stat;
int reg3_dyn;
int reg4_stat;
int reg4_dyn;
int i=1; //sample couter init
int pressedCal=0;//Cal has not been pressed
int Convert_A(int Analog) {
return 500 + 2 * Analog;
}
int Convert_D(int Digital) {
return 500 + 2048 * Digital;
}
int actualForce(int Sample) {
return k*Sample;
}
void memoryFull() {
digitalWrite(7, HIGH);
}
void runServo(int width) {
digitalWrite(13, HIGH);
delayMicroseconds(width);
digitalWrite(13, LOW);
delay(20);
}
// sensor value is somehow stored to SD
void storeSensor(int* value){
temp=value[1]; // dummy
}
// holdTime value is converted and affects updating speed
int holdTime(int value){
if (value=100) return 50; // dummy
}
// newUser enables new data sequence input
void newUser(){
i=1; //resets sample counter
digitalWrite(7, LOW); //switches off memoryFull LED
}
// Cal calibrates sensor to actual reference force (1kg)
int Cal(){
return 1; // dummy
}
void loop () {
reg1_stat=analogRead(0);
reg2_stat=analogRead(1);
reg3_stat=digitalRead(2);
reg4_stat=digitalRead(3);
// delays 40ms to await any knob changes.
delay(40);
// abs neccesary due to sampling accuracy.
reg2_dyn=analogRead(1);
if (abs(reg2_dyn-reg2_stat)>10){
runServo(Convert_A(reg2_dyn)); // used for debugging
pressedCal=0;//if other than Cal is pressed again
holdTime(reg2_dyn);
}
reg3_dyn=digitalRead(2);
if (reg3_dyn != reg3_stat){
runServo(Convert_D(reg3_dyn)); // used for debugging
pressedCal=0;//if other than Cal is pressed again
newUser();//resets i and LED
}
reg4_dyn=digitalRead(3);
if (reg4_dyn != reg4_stat){
runServo(Convert_D(reg4_dyn)); // used for debugging
pressedCal++;
if (pressedCal=2){ //Cal starts only on second press
k=Cal();//sets the global value of k
pressedCal=0;//resets Cal counter
}
}
// abs neccesary due to sampling accuracy.
reg1_dyn=analogRead(0);
if (abs(reg1_dyn-reg1_stat)>10){
runServo(Convert_A(reg1_dyn)); // used for debugging
pressedCal=0;//if other than Cal is pressed again
strengthArray[i]=actualForce(reg1_dyn);
i++;
if (i=501) {
memoryFull(); //turns on LED
storeSensor(strengthArray);//moves data to SD
}
}
}
Kod: Markera allt
// abs neccesary due to sampling accuracy.
sensor_dyn=analogRead(0);
if (abs(sensor_dyn-sensor_stat)>10){
strengthArray[i]=actualForce(sensor_dyn);
tempMaxForce=actualForce(sensor_dyn);
if (tempMaxForce>maxForce){
maxForce=tempMaxForce;
displayTmp(maxForce);//sends maxForce to display only if new maxForce
runServo(analogConvert(maxForce));// used for debugging
}
delay(peakHold); //holds maxForce
displayTmp(actualForce(sensor_dyn));//sends current value to display
runServo(analogConvert(sensor_dyn));// used for debugging
i++;
if (i=501) {
memoryFull(); //turns on red LED
storeSensor(strengthArray);//moves data to SD
}
}
}
Kod: Markera allt
/*
Poll
*/
//int servo=13;
//int PotValue;
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
pinMode(13, OUTPUT); //output for Servo/debugging
pinMode(6, OUTPUT); //green LED for Cal
pinMode(7, OUTPUT); //red LED for memoryFull
// initialize the digital pin as an input.
// Pin 2&3 are connected to a button tied to GND with pullup 10K to VCC.
pinMode(2, INPUT); //input for NewUser button
pinMode(3, INPUT); //input for Cal button
}
int temp;
int peakHold=10; //initiates minimum holdTime to 10ms
int tempCal; //temporary calibration variable
int tempMaxForce; //used for holdTime of maximum force
int strengthArray[500]; //array for converted samples (RAM=2kB, 1 value=10bit~2B)
int sensor_stat;
int sensor_dyn;
int holdTime_stat;
int holdTime_dyn;
int newUser_stat;
int newUser_dyn;
int cal_stat;
int cal_dyn;
int i=1; //sample counter init
int maxForce=0; //initiates maximum force to 0kg
int analogConvert(int Analog) {
return 500 + 2 * Analog;
}
int digitalConvert(int Digital) {
return 500 + 2048 * Digital;
}
void runServo(int width) {
digitalWrite(13, HIGH);
delayMicroseconds(width);
digitalWrite(13, LOW);
delay(20);
}
int actualForce(int Sample) {
return Sample;//gives four digits i.e hg-resolution of 0-102,3kg.
//return Sample/10; gives decimal point(?)
}
void memoryFull(int state) {
if (state==1)
digitalWrite(7, HIGH); //green@debug
if (state==0)
digitalWrite(7, LOW);
}
void highBlink() { //._.___
digitalWrite(6, HIGH);
delay(10);
digitalWrite(6, LOW);
delay(50);
digitalWrite(6, HIGH);
delay(10);
digitalWrite(6, LOW);
}
void lowBlink() { //.___
digitalWrite(6, HIGH);
delay(10);
digitalWrite(6, LOW);
delay(1000);
digitalWrite(6, HIGH);
delay(10);
digitalWrite(6, LOW);
}
void calBlink() { //ON
digitalWrite(6, HIGH);
}
// displays value on LCD (may be predefined as display())
int displayTmp(int value){
return 1;//dummy
}
// sensor value is somehow stored to SD
void storeSensor(int* value){
temp=value[1]; //dummy
}
void holdTime(int value){
peakHold=value+10;//delay cannot be zero
}
// newUser enables new data sequence input
void newUser(){
//digitalWrite(7, LOW); //switches off memoryFull LED
i=1; //resets sample counter
maxForce=0; //resets maxForce
memoryFull(0); //turns off red LED
}
// Cal calibrates sensor to actual reference force (1kg)
void Cal(){
tempCal=analogRead(0);//put 1kg of force before pressing Cal button
while ((tempCal>50) || (tempCal<5)) { //tune as closely as you can (50=debug)
if (tempCal>10) highBlink();//blinks green LED ._.___
if (tempCal<10) lowBlink();//blinks green LED .___
delay(500);//tune pot on sensor
tempCal=analogRead(0);
}
calBlink();//turns green LED on
i=1; //resets sample counter
maxForce=0; //resets maxForce
memoryFull(0); //turns off red LED
}
void loop () {
sensor_stat=analogRead(0);
holdTime_stat=analogRead(1);
newUser_stat=digitalRead(2);
cal_stat=digitalRead(3);
// delays 40ms to await any knob changes.
delay(40);
// abs neccesary due to sampling accuracy.
holdTime_dyn=analogRead(1);
if (abs(holdTime_dyn-holdTime_stat)>10){
//runServo(analogConvert(holdTime_dyn)); // used for debugging
holdTime(holdTime_dyn);// sets holdTime
}
newUser_dyn=digitalRead(2);
if (newUser_dyn != newUser_stat){
runServo(digitalConvert(newUser_dyn)); // used for debugging
newUser();//resets sample counter, memoryFull LED and maxForce
}
cal_dyn=digitalRead(3);
if (cal_dyn != cal_stat){
runServo(digitalConvert(cal_dyn)); // used for debugging
Cal();
}
// abs neccesary due to sampling accuracy.
sensor_dyn=analogRead(0);
if (abs(sensor_dyn-sensor_stat)>10){
strengthArray[i]=actualForce(sensor_dyn);
tempMaxForce=actualForce(sensor_dyn);
if (tempMaxForce>maxForce){
maxForce=tempMaxForce;
displayTmp(maxForce);//sends maxForce to display only if new maxForce
runServo(analogConvert(maxForce));// used for debugging
}
delay(peakHold); //holds maxForce
displayTmp(actualForce(sensor_dyn));//sends current value to display
runServo(analogConvert(sensor_dyn));// used for debugging
i++;
if (i==21) {//std=501
memoryFull(1); //turns on red LED
storeSensor(strengthArray);//moves data to SD
}
}
}