Array, For Loops, and AnalogWrite

Hi,

I’m fairly new to Arduino and I was trying out some stuff, and I happened to try this out but it does not do what its supposed to. I expect it to read the charecter at every index value and change the brightness accordingly every 1 second. However the brightness of the LED remains constant throughout. Any suggestions?

#include <Wire.h>
#include <Servo.h>
#include <seeed-kit.h>
#include <math.h>



char text[] = {"AZZA"};
void PassTextToLaser(char theText[]);
int Length = (sizeof(text)/sizeof(text[0]));
int Laser = 11;


int main() {

  init();
  lcdInit();
  PassTextToLaser(text);
  return 0;
}

void PassTextToLaser(char theText[])
{
  int ArrayForLaser[]= {};
  for( int i = 0; i < Length; i++)
  {
    if ( theText[i] == 'A')
    {
      ArrayForLaser[i]= 25;
    }
    else if ( theText[i] == 'B')
    {
      ArrayForLaser[i]= 66;
    }
    else if ( theText[i] == 'C')
    {
      ArrayForLaser[i]= 67;
    }
    else if ( theText[i] == 'D')
    {
      ArrayForLaser[i]= 68;
    }
    else if ( theText[i] == 'E')
    {
      ArrayForLaser[i]= 69;
    }
    else if ( theText[i] == 'F')
    {
      ArrayForLaser[i]= 70;
    }
    else if ( theText[i] == 'G')
    {
      ArrayForLaser[i]= 71;
    }
    else if ( theText[i] == 'H')
    {
      ArrayForLaser[i]= 72;
    }
    else if ( theText[i] == 'I')
    {
      ArrayForLaser[i]= 73;
    }
    else if ( theText[i] == 'J')
    {
      ArrayForLaser[i]= 74;
    }
    else if ( theText[i] == 'K')
    {
      ArrayForLaser[i]= 75;
    }
    else if ( theText[i] == 'L')
    {
      ArrayForLaser[i]= 76;
    }
    else if ( theText[i] == 'M')
    {
      ArrayForLaser[i]= 77;
    }
    else if ( theText[i] == 'N')
    {
      ArrayForLaser[i]= 78;
    }
    else if ( theText[i] == 'O')
    {
      ArrayForLaser[i]= 79;
    }
    else if ( theText[i] == 'P')
    {
      ArrayForLaser[i]= 80;
    }
    else if ( theText[i] == 'Q')
    {
      ArrayForLaser[i]= 81;
    }
    else if ( theText[i] == 'R')
    {
      ArrayForLaser[i]= 82;
    }
    else if ( theText[i] == 'S')
    {
      ArrayForLaser[i]= 83;
    }
    else if ( theText[i] == 'T')
    {
      ArrayForLaser[i]= 84;
    }
    else if ( theText[i] == 'U')
    {
      ArrayForLaser[i]= 85;
    }
    else if ( theText[i] == 'V')
    {
      ArrayForLaser[i]= 86;
    }
    else if ( theText[i] == 'W')
    {
      ArrayForLaser[i]= 87;
    }
    else if ( theText[i] == 'X')
    {
      ArrayForLaser[i]= 88;
    }
    else if ( theText[i] == 'Y')
    {
      ArrayForLaser[i]= 89;
    }
    else if ( theText[i] == 'Z')
    {
      ArrayForLaser[i]= {255};
    }

      analogWrite(Laser,ArrayForLaser[i]);
      delay(1000);

  }

}

Project1.1.ino (2.43 KB)

Any suggestions?

Post your code.
In code tags

  int ArrayForLaser[]= {};

Mark

holmes4:

  int ArrayForLaser[]= {};

Mark

I don't get what that means.

MSA23dsn:
I don't get what that means.

It means "WARNING! You are creating an array of ZERO elements. If you try to store any data in that array you will be writing off the end of your array and damaging other data."
Try:int ArrayForLaser[Length];

This doesn’t look like an Arduino sketch. It’s missing setup() and loop(), uses main() and return 0…

Here’s a modified version that should point you in the right direction:

//not required for this sketch
//#include <Wire.h>
//#include <Servo.h>
//#include <seeed-kit.h>
//#include <math.h>

//prototype modified to show us passing a pointer to the reference text string
void PassTextToLaser( char *pszText );

char text[] = {"AZZA"};

//this computation results in a value that includes the NULL termination. Subtract one (1) to
//get the number of printable characters.
const int Length = (sizeof(text)/sizeof(text[0])) - 1;

int Laser = 11;
int pinLED = 13;        //added for debug

//each element in this 26-byte array corresponds to a capital letter of the alphabet
//A -> 25
//B -> 66
//C -> 67
//...
//Z -> 255
//Each letter in the text[] string is used as an index into this table.
//more elegant than 26 if/else statements
const uint8_t
    LetterToPWMConversion[] = 
    {
        25, 66, 67, 68, 69, 70, 71, 72, 
        73, 74, 75, 76, 77, 78, 79, 80,
        81, 82, 83, 84, 85, 86, 87, 88,
        89, 255
         
    };

//you need a setup() in Arduino sketches to initialize things like output pins...
void setup()
{
    Serial.begin( 9600 );
    pinMode( Laser, OUTPUT );
    pinMode( pinLED, OUTPUT );
    Serial.println( Length );
    
}//setup

void loop() 
{
    //commented out code that has no corresponding functions in this sketch
    //init();
    //lcdInit();
    PassTextToLaser( text );

}//loop

//pass a pointer to the text string
void PassTextToLaser( char *pszText )
{
    int 
        value;
  
    //
    for( int i=0; i<Length; i++ )
    {
        //the index into the array (value) is derived from the character at the current letter (i)
        //minus the ASCII value of 'A'. So 'A' gives value = 0; 'B' gives value = 1 etc
        value = *(pszText + i) - 'A';
        
        Serial.println( value );    //print the index number for reference
        
        //write the value from the table to the PWM pin. I copy it to the LED pin for debug/visibility
        analogWrite( pinLED, LetterToPWMConversion[ value ] );
        analogWrite( Laser, LetterToPWMConversion[ value ] );
        
        delay(1000);
    
    }//for
  
}//PassTextToLaser

This doesn’t look like an Arduino sketch. It’s missing setup() and loop(), uses main() and return 0…

Find out what the IDE really does!. There is nothing wrong (as such) with the program.

Mark

Thank you! It works now