Pages: [1]   Go Down
Author Topic: Removing Serial.println() causes errors  (Read 428 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
  }
}
« Last Edit: July 14, 2009, 06:58:38 pm by Ayelix » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 290
Posts: 25735
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
interval = long((((val)-270)/360)*1000);
could be replaced by a "map" call http://arduino.cc/en/Reference/Map
Logged

"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.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 14
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
« Last Edit: July 15, 2009, 07:25:09 am by Ayelix » Logged

Pages: [1]   Go Up
Jump to: