system
March 23, 2010, 3:48pm
1
Sorry I'm new to this and I'm trying to setup my first project. I'm trying to improve upon someone else's project. He posted the code and it works for him but it doesn't do everything I want. I have 2 problems.
First When I startup the project the servos are in the last position they were in when the arduino lost power. Can you read in the position of the servo directly from the servo? My other option is to write the last known to a permanent variable but I think that would take a lot more coding and affect my second problem.
Problem 2 is I think my Arduino is running out of memory. The system goes out to a website grabs some values and then displays them with the servos. This works for 2 or 3 times and then darning the last download it seems to hang and not update the servos or continue downloading. I'm not sure why it would run out of memory since the variables are over written each time it downloads so there should be a build up of information any where. I can post the code if you want. It's based on the weather clock by Sean (google for weather clock sean)
Can you read in the position of the servo directly from the servo?
Not without physically hacking the servo, or connecting another pot to the output shaft, no you can't.
Problem 2 is impossible to say without seeing the code.
system
March 23, 2010, 3:56pm
3
I will post the code as soon as I can get to my laptop.
system
March 23, 2010, 8:52pm
4
Sorry about the mess. I haven't finished cleaning it up since I've been having the memory issue. It still compiles and is 16876 bytes, so it's no too big for the arduino.
#include <Flash.h>;
#include <Ethernet.h>;
#include <WString.h>;
#include <Servo.h>;
#include <Streaming.h>;
#define endl "\n";
//
// Ethernet variables
//
byte arduinoMac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte arduinoIp[] = { 10, 0, 0, 156 };
byte serverIp[] = { 199, 199, 209, 186 };
Server server(80);
Client fetchClient(serverIp, 80);
//
// Weather conditions
//
double temperature = 99;
int8_t windchill = 0;
String city = String(30);
String currentCondition = String(30);
String currentConditionLower = String(30);
int8_t currentConditionImage = 0;
String wind = String(15);
int8_t humidity = 101;
String forecastCondition = String(30);
String forecastConditionLower = String(30);
int8_t forecastConditionImage = 0;
int8_t forecastHigh = 99;
int8_t forecastLow = 99;
int8_t forecastPOP = 0;
int time = 1000;
int updateAfterTime = 700;
int updateBeforeTime = 2200;
boolean useWindchill = 0;
Servo servoCondition;
Servo servoTemperature;
uint8_t servoConditionValue = 180;
uint8_t servoTemperatureValue = 180;
byte maxLength = 75;
String inString = String(maxLength);
int updatecount = 57;
long previousMillis = 0;
void setup() {
// Start serial for debugging
Serial.begin(9600);
// Delay until ethernet initalizes
delay(5000);
// Start ethernet
Ethernet.begin(arduinoMac, arduinoIp);
server.begin();
servoTemperature.attach(5);
servoCondition.attach(6);
servoTemperature.detach();
servoCondition.detach();
}
void loop() {
if (millis() - previousMillis > 1000) {
previousMillis = millis();
// One second has elapsed, tick the clock.
updatecount++;
}
if (updatecount >= 60) {
fetchWeather();
}
Client client = server.available();
if (client) {
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (inString.length() < maxLength) {
inString.append(c);
}
if (c == '\n' && current_line_is_blank) {
if (inString.contains("?")) {
int Pos_a = inString.indexOf("a");
int Pos_b = inString.indexOf("b");
int Pos_c = inString.indexOf("c");
int End = inString.indexOf("H");
updateAfterTime = atoi(inString.substring((Pos_a+2), (Pos_b-1)));
updateBeforeTime = atoi(inString.substring((Pos_b+2), (Pos_c-1)));
useWindchill = atoi(inString.substring((Pos_c+2), (End-1)));
updateClock();
}
client << F("HTTP/1.1 200 OK") << endl;
client << F("Content-Type: text/html") << endl;
client << endl;
client << F("<html><head><title>");
client.print(city);
client <<F("</title><style type=\"text/css\">body{font-family:verdana,sans-serif;}</style></head><body>") << endl;
client << F("<h1>");
client.print(city);
client << F("</h1>") << endl;
client << F("<h2>Current Conditions</h2>") << endl;
client << F("<h3>");
client << F("<img src='http://justinm.net/weather/icons/") << currentConditionImage << F(".png'>");
client.print(currentCondition);
client << F("
Temperature: ") << temperature;
client << F("</h3>
") << endl;
client << F("<h2>Settings</h2>") << endl;
client << F("<form method=get>Only update the clock between <input type=text size=4 value=\"") << updateAfterTime << F("\" name=a> and <input type=text size=4 value=\"") << updateBeforeTime << F("\" name=b>
");
client << F("
<input type=submit value=Update></form>");
client << F("<p><i>Last updated: ") << time << F("</i></p>") << endl;
client << F("</body></html>") << endl;
break;
}
if (c == '\n') {
current_line_is_blank = true;
} else if (c != '\r') {
current_line_is_blank = false;
}
}
}
delay(25);
inString = "";
client.stop();
}
}
void fetchWeather() {
if (fetchClient.connect()) {
Serial << F("Reconnected");
fetchClient.println("GET http://justinm.net/weather/weather_parse.php HTTP/1.0");
fetchClient.println();
delay(2000);
updatecount = 0;
} else {
Serial << F("Reconnect failed");
updatecount = 55;
}
windchill = 0;
forecastHigh = 0;
forecastLow = 0;
forecastPOP = 0;
while (fetchClient.available()) {
char c = fetchClient.read();
if (inString.length() < maxLength) {
inString.append(c);
}
if (c == '\n') {
Serial.println(inString);
if (inString.startsWith("<temp>")) {
temperature = atof(inString.substring(6, inString.length()-4));
}
if (inString.startsWith("<cond>"))
currentCondition = inString.substring(6, inString.length()-6);
if (inString.startsWith("<time>"))
time = atof(inString.substring(6, inString.length()-6));
if (inString.startsWith("<city>"))
city = inString.substring(6, inString.length()-8);
Serial.println(temperature);
Serial.println(currentCondition);
Serial.println(time);
Serial.println(city);
inString = "";
}
}
fetchClient.stop();
updateClock();
}
void updateClock() {
/*Servo positions
166 = snow img 1
146 = sleet img 2
126 = sunny img 3
103 = partycloudy img 4
81 = cloudy img 5
58 = lightrain img 6
38 = rain img 7
18 = thundershowers img 8
*/
/* servoConditionValue = 90; */
currentConditionImage = 0;
forecastConditionImage = 0;
currentConditionLower = currentCondition;
currentConditionLower.toLowerCase();
forecastConditionLower = currentCondition;
forecastConditionLower.toLowerCase();
if (currentConditionLower.contains("cloudy")|| currentConditionLower.contains("fog")) {
servoConditionValue = 81;
currentConditionImage = 5;
}
if (currentConditionLower.contains("few clouds") || currentConditionLower.contains("haze") || currentConditionLower.contains("partly cloudy") || currentConditionLower.contains("partly sunny")) {
servoConditionValue = 103;
currentConditionImage = 4;
}
if (currentConditionLower.contains("fair") || currentConditionLower.contains("clear")|| currentConditionLower.contains("sunny")) {
servoConditionValue = 126;
currentConditionImage = 3;
}
if (currentConditionLower.contains("snow")) {
servoConditionValue = 166;
currentConditionImage = 1;
}
if (currentConditionLower.contains("mist") || currentConditionLower.contains("light rain") || currentConditionLower.contains("drizzle and fog") || currentConditionLower.contains("drizzle") || currentConditionLower.contains("mist") || currentConditionLower.contains("misty") || currentConditionLower.contains("drizzle")) {
servoConditionValue = 58;
currentConditionImage = 6;
}
if (currentConditionLower.contains("rain")) {
servoConditionValue = 38;
currentConditionImage = 7;
}
if (currentConditionLower.contains("freezing drizzle") || currentConditionLower.contains("freezing rain")) {
servoConditionValue = 146;
currentConditionImage = 2;
}
if (currentConditionLower.contains("heavy rain") || currentConditionLower.contains("thunderstorms")) {
servoConditionValue = 18;
currentConditionImage = 8;
}
if ((time > updateAfterTime)&&(time < updateBeforeTime)) {
if (useWindchill == 0)
servoTemperatureValue = (180 - ((temperature + 30) * 1.29));
else
servoTemperatureValue = (180 - ((windchill + 30) * 1.29));
// Sanity check
if (servoTemperatureValue < 10)
servoTemperatureValue = 10;
if (servoTemperatureValue > 175)
servoTemperatureValue = 175;
if (servoConditionValue < 10)
servoConditionValue = 10;
if (servoConditionValue > 175)
servoConditionValue = 175;
servoTemperature.attach(5);
servoCondition.attach(6);
servoCondition.write(servoConditionValue);
servoTemperature.write(servoTemperatureValue);
delay(500);
servoTemperature.detach();
servoCondition.detach();
}
}
system
March 23, 2010, 8:58pm
5
Could be running short of RAM.
Maybe investigate putting all those strings into PROGMEM.