I am trying to get code that runs on the uno to run on the Mega. The code I am running is based on the Cloud Hoppers weather station and I would like to know how to modify it to run on the mega I have tried for months. I am modifying the code to shut a greenhouse vent if a maximum wind speed is exceeded.
The anemometer is connected to D5.
The uno works connected to D5 the Mega does nothing on D5.
Thanks in Advance.
This is how I found the windspeed pulse counting code.
void windSpeed()
{
//start counting.............................................
//delay(100);
//digitalWrite(8, HIGH);
//delay(100);
//digitalWrite(8, LOW);
if (clock == 0) {
startTime = millis(); // read the current clock
bitSet(TCCR1B ,CS12); // counter clock source is externet pin
bitSet(TCCR1B ,CS11); // Clock rising Edge
clock = 1;
}
//delay(samplePeriod);
if ((millis() - startTime) >= 2000) //workout duration
{
peakCounter ++;
TCCR1B = 0; // stop counting
count = TCNT1;
TCNT1 = 0; //reset hardware counter
clock = 0;
if (peakCounter >=150) {
peakCounter = 0;
gust = peekWind; if (gust >200) gust = 0;
peekWind = 0;
}
numberOfCounts = count;
numberOfCounts = (1.6*numberOfCounts)/2;
if (numberOfCounts > peekWind) (peekWind = numberOfCounts);
// averaging stuff************************************************
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = numberOfCounts;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
if (average <= 0) average = 0;
//****************************************************************
}
The source code is at
I'm too lazy to check the snippet of code in detail - 99% of other folk are just going to tell you to post the full code. Secondly, what's the actual question? Do you want us to write your code for you? Even if we were minded to do so, we'd need a hell of a lot more information.
Ask yourself (then tell us) why the snippet does NOT run on the Mega? Lazily, I suspect it may be some hardware naming differences, or perhaps a different pin mapping. Either way, the answer will lie in the datasheet. I'm guessing you haven't read that.
I suggest reading and understanding at least the sections that relate to timer setup / register control and pin mapping.
My guess is that once you have done that, you may be able to answer your own question and/or modify the code yourself. If not, then come back with ALL your code, any error messages and a much more specific question.
bitSet(TCCR1B ,CS12); // counter clock source is externet pin
bitSet(TCCR1B ,CS11); // Clock rising Edge
This code is using the external clock source (input pin D5) on Timer 1, and the timer is being used as a pulse counter.
The Mega does not use the same pin. See this old thread for a discussion of available external clock source pins and the timers used on the Mega. External timer clock source. - Programming Questions - Arduino Forum
Here's also a reference on the pin mapping of the Mega. https://arduino-info.wikispaces.com/MegaQuickRef
Try change the code to use Timer5 which uses Pin 47 for external clock source input.
void windSpeed()
{
//start counting.............................................
//delay(100);
//digitalWrite(8, HIGH);
//delay(100);
//digitalWrite(8, LOW);
if (clock == 0) {
startTime = millis(); // read the current clock
bitSet(TCCR5B ,CS52); // counter clock source is externet pin
bitSet(TCCR5B ,CS51); // Clock rising Edge
clock = 1;
}
//delay(samplePeriod);
if ((millis() - startTime) >= 2000) //workout duration
{
peakCounter ++;
TCCR5B = 0; // stop counting
count = TCNT5;
TCNT5 = 0; //reset hardware counter
clock = 0;
if (peakCounter >=150) {
peakCounter = 0;
gust = peekWind; if (gust >200) gust = 0;
peekWind = 0;
}
numberOfCounts = count;
numberOfCounts = (1.6*numberOfCounts)/2;
if (numberOfCounts > peekWind) (peekWind = numberOfCounts);
// averaging stuff************************************************
// subtract the last reading:
total = total - readings[readIndex];
// read from the sensor:
readings[readIndex] = numberOfCounts;
// add the reading to the total:
total = total + readings[readIndex];
// advance to the next position in the array:
readIndex = readIndex + 1;
// if we're at the end of the array...
if (readIndex >= numReadings) {
// ...wrap around to the beginning:
readIndex = 0;
}
// calculate the average:
average = total / numReadings;
if (average <= 0) average = 0;
//****************************************************************
}
Thank you cattledog for your very helpful comments I have tried the code but does not display the windspeed. I will try to resolve the problem myself as I am learning and new to the arduino card and the forum. Baremetals comments have put me off reposting although cattledog you understood my question.
Excellent all fixed the alterations made by cattledog work on the mega. The problem it did not display was in the code elsewhere.
Thank you cattledog for your time and the above references which I have read and saved for further reference.
kezznorton:
The problem it did not display was in the code elsewhere.
That's why it's always best to post the whole code Sorry if my comments put you off, I was just trying get you to think through the problem logically yourself. Anyway, seems like its all fixed now, so alls well that ends well.