Problem With Array

  if(millis() - lastReadingDelay > readingDelay){
    i = i ++;
    //    ii = ii ++;

    bS = bulkSpeed[i];
    //     pD = protonDensity[i];
    bS = constrain(bS, 2883, 3000);
    bS = map(bS, 2883, 3000, 180, 255);
    lastReadingDelay = millis();
    Serial.println(bS);
    analogWrite(motorSpeedPin, bS);

  }

Anyone know why this doesn't work? I can't get the analogWrite to us the bS variable. Further, whenever I try to Serial.print anything other than Serial.println(bS), i.e. Serial.print("bulkSpeed = "), I just get a bunch of jibberish in my Serial monitor.

We like to see the whole sketch. I don't know what kind of variables you use (integer, long or float), and I don't know how you use those millis variables, or if 'i' is set to zero... and so on. Did you set the baudrate of the serial monitor (on the PC) the same as in the setup function ?

Here’s the semi-full sketch. The full one has two sets of arrays, each with 1200 numbers.

#include <PWM.h>

int dir = 12;    
int motorSpeedPin = 9;
int32_t frequency = 20000; //frequency (in Hz)

int forward = LOW;
int reverse = HIGH;

int motorSpeed = 180;


int numDensityReading = 1200;
int protonDensity[] = {
  1.5, 1.5, 1.2, 
  1.1, 
  1.2, 
  1.6, 
  1.4, 
  1.6, 
  -9999.9, 
  -9999.9, 
  1.8, 
  2.1, 
  1.5, 
  1.8, 
  1.7, 
  1.3, 
  1.5, };


int numSpeedReadings = 1200;
int bulkSpeed[] = {
  3346, 3320, 3340, 
  3334, 
  3301, 
  3287, 
  3188, 
  3156, 
  2836, 
  2875,  };

int i = 0;
int ii = 0;
int bS;
int pD;


long lastReadingDelay = 0;
long readingDelay = 10000;

void setup(){
  Serial.begin(9600);
  InitTimersSafe(); 
  bool success = SetPinFrequencySafe(motorSpeedPin, frequency);
  if(success) {
    Serial.println("Frequency Successfully Set.");
  }
  pinMode(dir, OUTPUT);
  //  pinMode(button0, INPUT_PULLUP);
  //  pinMode(button1, INPUT_PULLUP);
  digitalWrite(dir, forward);

}

void loop(){

  if(millis() - lastReadingDelay > readingDelay){
    i = i ++;
    //    ii = ii ++;

    bS = bulkSpeed[i];
    //     pD = protonDensity[i];
    bS = constrain(bS, 2883, 3000);
    bS = map(bS, 2883, 3000, 180, 255);
    lastReadingDelay = millis();
    //    Serial.print("bulkSpeed = ");
    Serial.println(bS);
    //    Serial.print(",");
    //    Serial.println(pD);
    analogWrite(motorSpeedPin, bS);

  }




}

You use the variable 'i', but it is never reset to zero, so it will read beyond the array. The array protonDensity[] is an integer array, but you try to fill it with float numbers. Did you set the serial monitor to 9600 baud ? Can you write a message in setup() to see if it works ? I think you need to set motorSpeedPin to OUTPUT in setup(). What PWM library are you using ?

Ok, thanks for the heads up about i reseting it back to 0, I completely forgot to do that. I'm planning on multiplying those floats by 10 so that they're ints before coming into arduino, I just haven't done it yet, so I let the arduino round it for me just for testing. Serial monitor is 9600, and the analogWrite function works fine if I'm just using it in the loop without the array business. I've never had to initiate analogWrite pins as output, and when I have it causes problems. Also, just to be sure it wasn't the library screwing with things, I removed it and I'm having the same issue.

Okay, analogWrite doesn't need pinMode :blush: http://arduino.cc/en/Reference/analogWrite : "You do not need to call pinMode() to set the pin as an output before calling analogWrite()".

Your problem is weird. Do you use the newest Arduino IDE 1.0.5 (or the beta) ? Perhaps you can make a few test sketches without the motor connected. First thing is the Serial communication, you need that working well, so you can write variables to your screen.

I prefer to have the variables for timing and millis to be unsigned long instead of long. I can't find any other problem.

The full one has two sets of arrays, each with 1200 numbers.

That would fill up your complete RAM on an UNO.

Which board are you using? (did I miss that info?)

Ah, possibly. It's the pro mini 5v.

from specs - http://arduino.cc/en/Main/ArduinoBoardProMini -

Microcontroller ATmega168 Operating Voltage 3.3V or 5V (depending on model) Input Voltage 3.35 -12 V (3.3V model) or 5 - 12 V (5V model) Digital I/O Pins 14 (of which 6 provide PWM output) Analog Input Pins 8 DC Current per I/O Pin 40 mA Flash Memory 16 KB (of which 2 KB used by bootloader) SRAM 1 KB EEPROM 512 bytes Clock Speed 8 MHz (3.3V model) or 16 MHz (5V model)

no chance to cram 2400 ints in that one (without compression)