Pages: [1]   Go Down
Author Topic: PWM problem with a code part.  (Read 331 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well i am using some mosfet shield and the pwm signals
the idea i have is that one signal over serial will controll the light behaviour of 6 pwm pins ( 3 5 6 9 10 11)
The code accepts input as from 0 to 65 to dim leds this works nice
However i also wanted to create a slow blinking led, codes input 66 to 71  (slow) input (72 to 77) faster and input 78 to 83 fastest

i 've been looking over it for some hours nows but i dont see whats wrong here, is there perhaps a limit on the arduino ??

Here below is the complete code:

Code:

int pwmport = 0;
int pwmval = 0;
//char input = 66;
int input =0;

int pwmpin[]={3,5,6,9,10,11 }; //an array is a collection of nummer in this case pwm pins of arduino
int mode[]  ={0,0,0,0,0,0};
int counter = 0;

void setup()
{
 // >> see switch mode function    
Serial.begin(57600);
}

void loop()
{
  
 // input= Serial.read();
 // serial.println(i)


  input = 79;  // THIS EQUALS SET PWM PIN 11 in BLINK MODE

   pwmport = input % 6;
  if ( input<66)
  { pwmval = input * 10 / 6;
    analogWrite ( pwmpin[pwmport],pwmval );
    mode[pwmport] = 0;  }
    
  Serial.print ("set pin ");
  Serial.print(pwmport);
  Serial.print(" in mode ");
 
  if ((input>65)&&(input<72)){ mode[pwmport] = 1;Serial.println(1);}
  if ((input>71)&&(input<78)) {mode[pwmport] = 2;Serial.println(2);}
  if ((input>77)&&(input<84)) {mode[pwmport] = 3;Serial.println(3);}


  ++counter;
  
  for (int i=0;i<6;i++)
  { Serial.print("pin ");
    Serial.print(i);
    Serial.print("  mode=");
    Serial.print(mode[i]);Serial.print(" ");
    Serial.print("value=" );
    Serial.println(abs( -255 + (counter % 510 )));
    switch  (mode[i]) // i is the pin mode[i] is the mode for that pin
    {
    case 0:
           ;  // do nothing it is handled above
         break;
    case 1:
         analogWrite (i , abs( -255 + (counter *1 % 510 )));
         break;
    case 2:
         analogWrite(i , abs( -255 + ((counter * 2 ) % 510 )));
         break;
    case 3:
         analogWrite(i , abs( -255 + ((counter * 4 ) % 510 )));
         break;
    default:
         Serial.print("Error");
    }    
      
  }
  delay(1000); // 1  second
}
« Last Edit: April 11, 2013, 05:06:43 pm by PGT » Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The code you posted is indented horridly. While there is debate over whether the { goes on the line with the statement, or on a new lines, there is NO debate that NOTHING follows the { on the same line.

Likewise, there is NO debate about the placement of the }. It goes on a line all by itself.

Spaces between clauses in for statements make them easier to read.

But, the biggest problem is that you didn't tell us what the problem is. That code, however ugly, does something. You want it to do something. You did not tell us what either of those somethings is, though.
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

solved
analogWrite(pwmpin , abs( -255 + ((counter * 32 ) % 510 )));
Logged

Offline Offline
Full Member
***
Karma: 2
Posts: 219
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@PaulS

just wait what you program when you switch between multiple programming languages, in short its vs2010 C# style..

and since its only text it compiles the same.. when you are used to C# its ok

typo
analogWrite(pwmpin , abs( -255 + ((counter * 32 ) % 510 )));
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 610
Posts: 49080
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
just wait what you program when you switch between multiple programming languages, in short its vs2010 C# style..
I do switch languages quite often. Proper placement of curly braces is the same in C++ as in C, C##, Java, and every other language that uses them.

Just because Microsoft set the bar on the floor doesn't mean you can't raise it. Not that your style comes anywhere near what VS 2010 C# does for me.
Logged

Pages: [1]   Go Up
Jump to: