# Reading 1023 value to 255

Is this correct? serial.print(val / 4); ? I'm getting a reading of 0-1023 and want to convert that to 0-255

analogWrite(motor1Pin, val1 / 4);
Serial.println(val1);

That will work best, but for more versatility with odd scaling try this out…

/* Map an analog value to 8 bits (0 to 255) */
#include <SoftwareSerial.h>

void setup() {
Serial.begin(9600);
}

void loop()
{
val = map(val, 0, 1023, 0, 255);  //Scales 0-1023 to 0-255 (or whatever you want)
Serial.println(val,DEC); //Prints as decimal, with line feed
}

http://www.arduino.cc/en/Reference/Map

still not figuring it out...?

One caveat with map: Dividing by four and map are not interchangable. With map, zero, 85, and 170 are output for five input values. 255 is output for only a single input. In other words, the output from map is not uniform. Dividing by four is uniform.

ah i c ;) I'm still getting errors:

void loop()
{
{
val = map(val1, 0, 1023, 0, 255);
analogWrite(motor1Pin, val1);
Serial.println(val, DEC);

error: val not declared in this scope

const int switchPin = 2; // switch connected to digital pin 2
int pot1 = 0; // pot1 connected to analog 0
int pot2 = 1; // pot2 connected to analog 1
int pot3 = 2; // pot3 connected to analog 2
int val1 = 0;
int val2 = 0;
int val3 = 0;
int motor1Pin = 5; // h bridge pin connected digital 5
int motor2Pin = 6; // h bridge pin connected to digital 6
int motor3Pin = 10; // hbridge pin connected to digital 10
int enablePin = 9; //h bridge enable pin connected to digital 9
int enablePin1 = 12; // hbridge enable pin connected to digital 12

void setup()
{
Serial.begin(9600);
pinMode(switchPin, INPUT);
pinMode(motor1Pin, OUTPUT);
pinMode(motor2Pin, OUTPUT);
pinMode(enablePin, OUTPUT);
digitalWrite(enablePin, HIGH);
digitalWrite(enablePin1, HIGH);

}
void loop()
{
analogWrite(motor1Pin, val1 / 4);
Serial.println(val1);

analogWrite(motor2Pin, val2 / 4);
Serial.println(val2);

analogWrite(motor3Pin, val3 / 4);
Serial.println(val3);
}
else {

analogWrite(motor1Pin, 100);
delay(2000);
analogWrite(motor1Pin, 0);
delay(2000);
analogWrite(motor2Pin, 100);
delay(2000);
analogWrite(motor2Pin, 0);

}
}

Reason why I need to read 0-255 from the pots so I can configure at what range I need to analogWrite to through pwm so I wont have to compile forever when I can get a reading off the value of the pot…
Basically I’m turning the motor on the mirrors at certain speeds for certain affects with my spirograph I’m building. Thought itd be easier to manually adjust the pots read the values (0-255) then I know what value I can analogWrite to in my if statement

You should use val, not val1 in the analogwrite statement.

If you have a switch on switchpin 1 you should also debounce that switch, otherwise you could easily get confusing results.

Edit:

did you just forget the "1" where you use val, so you meant to write val1??

Othervise you need to declare val first

One caveat with map: Dividing by four and map are not interchangable. With map, zero, 85, and 170 are output for five input values. 255 is output for only a single input. In other words, the output from map is not uniform. Dividing by four is uniform.

Yes, you are incorrect.

hmmm...? still playing with it but its reading 1023 heres what I have

analogWrite(motor1Pin, val1 / 4);
Serial.println(val1 /4);

analogWrite(motor1Pin, val1 / 4); This should write out 0-255 on the PWM output

Serial.println(val1 /4); This should print 0-255 on the serial port

What part's not working?

its reading up to 1023 not 255

n/m it just started working right lol... dunno how tho

Yeah you really should be using map with that. dividing by four is just a bad way to do it.

Before you know it, you will be trying to divide by some ridiculous number in another project.

Mowcius

one last question How can I print in 3 columns? So I can read better? ;P

edit: How would the map way go then?

There might be a better way, but this works:

Serial.print(val1/4);  //no newline char
Serial.print(" ");        //space
Serial.print(val2/4);  //no newline char
Serial.print(" ");        //space
Serial.println(val3/4);  //last one has the newline '\n' as well

Here you go. I don't think there are any errors.

{
val1 = map(val1, 0, 1023, 0, 255);
analogWrite(motor1Pin, val1);
Serial.print(val1);
Serial.print(" ");

val2 = map(val2, 0, 1023, 0, 255);
analogWrite(motor2Pin, val2);
Serial.print(val2);
Serial.print(" ");

val3 = map(val3, 0, 1023, 0, 255);
analogWrite(motor3Pin, val3);
Serial.println(val3);
}

Mowcius

ty mowcus ill give it a try

One caveat with map: Dividing by four and map are not interchangable. With map, zero, 85, and 170 are output for five input values. 255 is output for only a single input. In other words, the output from map is not uniform. Dividing by four is uniform.

No I'm sorry, you are incorrect.

You might want to perform some testing before declaring that I've made a mistake.

its working thanks

Oh good :)

Mowcius