Delay не далет
- Войдите на сайт для отправки комментариев
Пнд, 14/07/2014 - 17:27
Здравствуйте.
Arduino Nano
Сегодня решил покрутить вентилятор.
Пищу вот такой скеч:
int motorPin = 9;
int led = 13;
int hallsensor = 2;
int NbTopsFan;
int Calc;
typedef struct{ //Defines the structure for multiple fans and their dividers
char fantype;
unsigned int fandiv;
}fanspec;
//Definitions of the fans
fanspec fanspace[3]={{0,1},{1,2},{2,8}};
char fan = 1; //This is the varible used to select the fan and it's divider, set 1 for unipole hall effect sensor
//and 2 for bipole hall effect sensor
void rpm () //This is the function that the interupt calls
{
NbTopsFan++;
}
void setup()
{
//Set the PWM Motor pin as an output
pinMode(motorPin, OUTPUT);
pinMode(led, OUTPUT);
pinMode(hallsensor, INPUT);
Serial.begin(9600);
attachInterrupt(0, rpm, RISING);
}
void hallinfo ()
{
NbTopsFan = 0; //Set NbTops to 0 ready for calculations
sei(); //Enables interrupts
delay (1000); //Wait 1 second
cli(); //Disable interrupts
Calc = ((NbTopsFan * 60)/fanspace[fan].fandiv); //Times NbTopsFan (which is apprioxiamately the fequency the fan is spinning at) by 60 seconds before dividing by the fan's divider
Serial.print (Calc, DEC); //Prints the number calculated above
Serial.print (" rpm\r\n"); //Prints " rpm" and a new line
}
void loop()
{
digitalWrite(led, HIGH);
hallinfo();
//Increase Motor Speed from 0 -> 255
analogWrite(motorPin, 0);
delay(10);
for (int i=50; i<=255; i++)
{
analogWrite(motorPin, i);
delay(1000);
}
digitalWrite(led, LOW);
delay(10000); //Hold it!
hallinfo();
digitalWrite(led, HIGH);
//Decrease Motor Speed from 255 -> 0
for(int i=255; i>=50; i--)
{
analogWrite(motorPin, i);
delay(1000);
}
analogWrite(motorPin, 0);
delay(10);
hallinfo();
digitalWrite(led, LOW);
delay(10000); //Hold it!
hallinfo();
}
delay от 1000 и более не работают.
Прошелся поиском по форуму, встречаются статьи типа "Отказ от delay()".
Но так и не понял, что мешает программе ждать.
P.S.: в программировании не новичок (pascal), но в программировании под МК новичок.
Как заголовок поправить?
Какой у Вас делей не работает?
В простешем скече (диодиком поморгать) работает?
Не вникая в детали Вашего кода - у Вас прерывания используются. Сюда и копайте.
//cli(); //Disable interrupts