trouble varying pwm in sample program

Hi everybody. I'm new to the ardunio platform, so i'm trying to teach my self how to program this thing to vary the pwm using 5 inputs (A0 though A4). that will be later . right now i am attepting varying the pwm just using A0 and A1. reading the serial port i just get the thrid setting i've established. any suggestions? Here's my code so far

int keyVal1 = 0;
int keyVal2 = 0;
int keyVal3 = 0;
int keyVal4 = 0;
int keyVal5 = 0;
int PWM = 0;
void setup(){
Serial.begin(300); // diagnostic tool
pinMode(13, OUTPUT); // led output
pinMode(12, OUTPUT); // 12-4 key in
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
}
void loop(){
digitalWrite(12, HIGH); // turn on the keys (matrix input)
digitalWrite(11, HIGH);
digitalWrite(10, HIGH);
digitalWrite(9, HIGH);
digitalWrite(8, HIGH);
digitalWrite(7, HIGH);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
keyVal1=analogRead(A0); // read the matrix output
keyVal2=analogRead(A1);
keyVal3=analogRead(A2);
keyVal4=analogRead(A3);
keyVal5=analogRead(A4);
if(keyVal1>500, keyVal2<500);
{
PWM=255;
}
if(keyVal1<500, keyVal2>500);
{
PWM=255/1.1;
}
if(keyVal1<500, keyVal2<500);
{
PWM=255/1.2;
}
//if(keyVal3>0)
//{
//PWM=255/4;
//}
//if(keyVal4>0)
//{
//PWM=255/8;
//}
//if(keyVal5>0)
//{
//PWM=255/16;
//}

Serial.print("pwm reads"); // diagnostic tool
Serial.println(PWM);
analogWrite(13, PWM);
Serial.println("A0 reads");
Serial.println(keyVal1);
Serial.println("A1 reads");
Serial.println(keyVal2);
delay(5000);
}

PWM=255/1.1;
 }
 if(keyVal1<500, keyVal2<500);
 {
   PWM=255/1.2;

Using floating point numbers with int expressions will almost always not give you the results you expect.

Lefty

There are several problems with this code. First,

if(keyVal1>500, keyVal2<500);

The , does not mean and. && does.
The ; on the end defines the body of the if block. I doubt that is what you want. Remove the ;

 analogWrite(13, PWM);

You can only use analogWrite successfully on the pins labeled PWM. On the Duemilanove and similar boards, pin 13 is not a PWM pin.

i am attepting varying the pwm just using A0 and A1.

What is connected to these two pins?

if it makes any sence, i am clamping the pins high or low to test my theroy. i don't care what comes out of the pwm right now. i am just trying to get the pwm to follow my movements. i am using an uno board, pin 13 does work as a pwm output, i already proved that to myself with a previous program i wrote about 2 hours ago.

A PWM function, when used in context of an Arduino, implies using the core library function called analogWrite() ( analogWrite() - Arduino Reference ) Which is only supported on certain pins depending on processor model as the library utilizes internal timers that only attach to certain output pins.

On most Arduino boards (those with the ATmega168 or ATmega328), this function works on pins 3, 5, 6, 9, 10, and 11. On the Arduino Mega, it works on pins 2 through 13. Older Arduino boards with an ATmega8 only support analogWrite() on pins 9, 10, and 11.

Your using the term PWM in it's broader form. Any output pin can be used for "bit banging" a pwm output signal.

Lefty

No it doenst, its impossible to do pwm in pin13, well its possible, with soft pwm, but neither you have done that or the arduino core as that.

i can re-assign the pwm output pin later. i changed the commas in the if statemens to &&. the pwm still stays at a given rate. that is not what i am trying to accomplish right now. i am trying to get the pwm to change according to the inputs i am using.

i am trying to get the pwm to change according to the inputs i am using.

At the bottom of your code, you are using Serial.print() to output values. Use that same technique to output the values read from the analog pins, and in each if block. It will quickly become obvious where your problem(s) are.

When people suggest code changes, it's nice to say whether you changed your code, or not. If not, explain why not. If you did, post the new code, so we can see whether there are other software problems.

i am sorry if there is any confusion as to what i am trying to do here. all i want is for the arduino to pick one pwm setting per program rotation. now having said that.

the bottom of the code uses serial print to display A0, A1 , and PWM. i stated i did change the code, on lines 35, 39, and 43 i changed the , to &&. the rest of the code is exactly the same.
keyVal1 is A0
keyVal2 is A1
here's the end lines:
if(keyVal1>500 && keyVal2<500);
{
PWM=255;
}
if(keyVal1<500 && keyVal2>500);
{
PWM=255/1.1;
}
if(keyVal1<500 && keyVal2<500);
{
PWM=255/1.2;
}

Serial.print("pwm reads"); // diagnostic tool
Serial.println(PWM);
// analogWrite(13, PWM);
Serial.println("A0 reads");
Serial.println(keyVal1);
Serial.println("A1 reads");
Serial.println(keyVal2);
delay(5000); // diagnostic tool
}

i am clamping the pins high or low to test my theroy.

What does this mean? Exactly how are you doing this?

When you printed out keyVal1 and keyVal2, what values did you see?

What values do you see for PWM?

It's hard to help you when you don't provide enough information.

Your last posted code still had semicolons on the 'if' statements. (see reply #2)
Remove them.
You've also still got your floating-point divide, which you'll have to change.

Please use the # (code) icon on the editor's toolbar when posting code.

by clamping the inputs i mean i am applying either +5v or grnd to the pin directly. this is still just a test. on the values i get: PWM- 221 A0 if tied to grnd 0 if tied to 3.3vdc 692 A1 if tied to grnd 0 if tied to +5vdc 1023. like i said i am trying to get the ardunio to pick one instance or another. here's the code redone:

int keyVal1 = 0;
int keyVal2 = 0;
int keyVal3 = 0;
int keyVal4 = 0;
int keyVal5 = 0;
int PWM = 0;
void setup(){
  Serial.begin(300);  // diagnostic tool
  pinMode(13, OUTPUT);  // led output
  pinMode(12, OUTPUT);  // 12-4 key in
  pinMode(11, OUTPUT);
  pinMode(10, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(4, OUTPUT);
}
void loop(){
  digitalWrite(12, HIGH);  // turn on the keys (matrix input)
  digitalWrite(11, HIGH);
  digitalWrite(10, HIGH);
  digitalWrite(9, HIGH);
  digitalWrite(8, HIGH);
  digitalWrite(7, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(5, HIGH);
  digitalWrite(4, HIGH);
  keyVal1=analogRead(A0);  // read the matrix output
  keyVal2=analogRead(A1);
  keyVal3=analogRead(A2);
  keyVal4=analogRead(A3);
  keyVal5=analogRead(A4);
  if(keyVal1>500 && keyVal2<500)
  {
    PWM=255;
  }
  if(keyVal1<500 && keyVal2>500)
  {
    PWM=255/1.1;
  }
  if(keyVal1<500 && keyVal2<500)
  {
    PWM=255/1.2;
  }

  Serial.print("pwm reads");  //  diagnostic tool
  Serial.println(PWM);
  // analogWrite(13, PWM);
  Serial.println("A0 reads");
  Serial.println(keyVal1);
  Serial.println("A1 reads");
  Serial.println(keyVal2);
  delay(5000);
}

on the values i get: PWM- 221

You get a negative number?
Can you confirm that value?

no i do not get a negetive number. that was just to say PWM dash interger. i have yet to attempt getting a negitive number on the board. btw i started using this thing on monday

This will go a lot better when you actually cut and paste some output from the serial monitor.

okay i just hooked up the board again heres what i got:

pwm reads231
A0 reads
310
A1 reads
656
pwm reads231
A0 reads
323
A1 reads
655
pwm reads231
A0 reads
1023
A1 reads
656
pwm reads231
A0 reads
0
A1 reads
656
pwm reads231
A0 reads
1023
A1 reads
655
pwm reads255
A0 reads
1023
A1 reads
0
pwm reads212
A0 reads
0
A1 reads
0

again, i am varying the inputs to try to have the program select one of 3 outputs.

btw i started using this thing on monday

And you haven't found the shift key yet?

again, i am varying the inputs to try to have the program select one of 3 outputs.

I guess I'm missing something. Given the values read from analog pins 0 and 1, which appear to sometimes be floating, you are getting a correct value for PWM. So, what seems to be the problem?

okay let's look at this at a diffrent angle: if i had an on-on switch (2 position) and told the arduino to change the pwm according to the switch setting. how would i program that into the program, using the analog inputs.

if i had an on-on switch (2 position) and told the arduino to change the pwm according to the switch setting.

Switches are typically digital devices - on or off. Reading them using analog pins is generally not the way to go.

If you have a link to the switch in question, I'd be happy to look at it, and make some recommendations.