Map Function, Serial.Print not working

Hi all. I’ve been hashing out some test code for my Arduino to talk to the servo. This is just to test it.

I’d like to be able to teach my students how to control a motor through the CloudBit, but first we are testing it with a dimmer.

I’ve run into the problem that it will not use the MAP function. In fact, even if I Serial.Print just (DIMMER) I get nothing or a constant number.

This happened even with NO MOTOR attached or with an LED OUTPUT. There is something wrong here and I need help figuring it out.
Here is the code.

// These constants won't change:

const int Dimmer = A1; // pin that the sensor is attached to
const int servo = 5; // pin that the LED is attached to
const int threshold = 50; // an arbitrary threshold level that’s in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(servo, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
int DimmerValue = analogRead(Dimmer);
DimmerValue = map(DimmerValue, 0, 1023, 0, 255);
Serial.print(DimmerValue); // print the analog value:

// if the analog value is high enough, turn on the motor:
if (DimmerValue > threshold) {
digitalWrite(servo, HIGH);
} else {
digitalWrite(servo, LOW);
}
delay(100); // delay in between reads for stability
}

Ok. I changed it up a bit. I am NOW getting numbers in the serial monitor. I have no idea why or what happened.

Here is the new code, MAP looks like its working though it’s fairly slow.

// These constants won’t change:
const int Dimmer = A1; // pin that the sensor is attached to
const int servo = 5; // pin that the LED is attached to
const int threshold = 50; // an arbitrary threshold level that’s in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(servo, OUTPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
int DimmerValue = analogRead(Dimmer);
DimmerValue = map(DimmerValue, 0, 1023, 0, 8);
Serial.print(DimmerValue);
Serial.print(’\n’);
// print the analog value:

delay(100); // delay in between reads for stability
}

Here is what it looks like on the serial monitor. It breaks and has spaces even though I have a carriage return. Any thoughts? Is it just the serial monitor or is my data actually looking like this?

1 Like

Hi, @alison_bits :smile:
I gave your program a run on my end, but I didn’t get the same problematic outcome. Here you can see my serial monitor with your original code on top:

It might be a shot in the dark, but perhaps setting the pinmode for the Dimmer to INPUT will help?

One habit I find useful whenever mapping is to print both the analogRead value and the mapped value. It gives a little more information and sometimes clarifies what is happening.

Below I’ve made both changes. I hope it helps!

// These constants won't change:
const int Dimmer = A1; // pin that the sensor is attached to
const int servo = 5; // pin that the LED is attached to
const int threshold = 50; // an arbitrary threshold level that's in the range of the analog input

void setup() {
// initialize the LED pin as an output:
pinMode(servo, OUTPUT);
// initialize the Dimmer pin as an input:
pinMode(Dimmer, INPUT);
// initialize serial communications:
Serial.begin(9600);
}

void loop() {
// read the value of the potentiometer:
int DimmerValue = analogRead(Dimmer);
int MapDimmerValue = map(DimmerValue, 0, 1023, 0, 8);
Serial.print(DimmerValue);
Serial.print('\t');
Serial.println(MapDimmerValue);
// print the analog value:

delay(100); // delay in between reads for stability
}
1 Like

Hi @alison_bits and @JackANDJude,

I also checked the code and it is working fine, but a funny outcome in my serial print monitor was caused by a wacky connection of the dimmer…
After cleaning the contacts between the dimmer and the Arduino it was all OK. :grinning:

2 Likes

Thanks all. I’m teaching a class and man is it hard to figure out what is going wrong sometimes, even when it’s simple. Cheers!

1 Like