Go Down

Topic: Removing Serial.println() causes errors (Read 535 times) previous topic - next topic

Ayelix

Jul 15, 2009, 01:58 am Last Edit: Jul 15, 2009, 01:58 am by Ayelix Reason: 1
For my second attempt with Arduino (Blink being the first), I tried combining the AnalogInput and BlinkWithoutDelay examples to make the blink speed controlled by a potentiometer.  I got it working, and I had a Serial.println(interval); in there for debugging.  Once I took it out, the blinking would start at the correct interval, then would quickly speed up and go constant-on (or a very small interval).  With the attached code, can anybody help me?  Thanks!
Quote
#define potPin 2
#define ledPin 13
float val = 0;
float interval = 0;
long previousMillis = 0;
int value = LOW;

void setup() {
 pinMode(ledPin, OUTPUT);
 Serial.begin(9600);
}

void loop() {
 //280-630 : Approxamate range of my pot
 val = analogRead(potPin);
 interval = long((((val)-270)/360)*1000);  //Change the val to an interval, approx 0-1000
 Serial.println(interval);  //The debug line that, when removed, causes error
 if (millis() - previousMillis > interval) {
   previousMillis = millis();

   if (value == LOW)
     value = HIGH;
   else
     value = LOW;

   digitalWrite(ledPin, value);
 }
}

AWOL

Can't see what's wrong right off, but I'd be inclined to use "long" for "interval" and "val", instead of "float" (floats are generally very much slower than integer data types).
This line:
Code: [Select]
interval = long((((val)-270)/360)*1000);
could be replaced by a "map" call http://arduino.cc/en/Reference/Map
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Ayelix

#2
Jul 15, 2009, 02:24 pm Last Edit: Jul 15, 2009, 02:25 pm by Ayelix Reason: 1
Yeah, I found map after posting this.  They were float because integers/longs wouldn't work properly for some reason (you can tell I'm new to this).  Anyways, I changed the setup to add in the Calibration example and used map for the interval, and now it works.  Thanks for the reply

Go Up