Give this Voltage for that Time (repeat)

Hello dear Friends of the Bits and Bytes,

Im currently longing to implement a rather simple task into my Arduino uno.

void setup(){
float Freqs[] = {5, 4, 3, 2, 1, 2, 3, 4, 5};      
float Freq = 3.4      // Average frequency of the above           
float T = 0.29       // Average Period (1/freqs)
pinMode(3, OUTPUT);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
int POS1 = digitalread(A2);                // read analog pin A2 as digital and set it to a variable
int POS2 = digitalread(A0); 
unsigned long lastUpdate
}

void loop()
  {
int i;
for (i = 0; i < 18) {
float pwm = (255*freqs[i])/5                    
float Tpwm = ((1/Freq)*(Freq/Freq[i])  
  }
  if(POS1 == HIGH)  // time to start
 {
lastUpdate = millis();
analogWrite(3, pwm)

    if((millis() - lastUpdate) > Tpwm)  // time to update
    {
      lastUpdate = millis();
      analogWrite(3, pwm);
       i=i+1; }
      
      if else((POS2 == HIGH) || ((millis() - lastUpdate) > T)) 
      {  
          int j;
          for (j = 0; j < 18) {
          float pwm2 = (255*freqs[j])/5            
          float Tpwm2 = (1/Freq)*(Freq/Freq[j])

lastUpdate = millis();
analogWrite(3, pwm2)

          if((millis() - lastUpdate) > Tpwm2)
      {lastUpdate = millis();
       analogWrite(3, pwm2);
       j=j+1;}
if j == 17{ {  while(1) { }; } // end programm.

Ill run you trough; First we set Variables, Inputs, and Outputs. Then we have pwm and Tpwm, which correspond to a certain Voltage for a relative Time.
Now what id like to see happen. Proggramm starts; It cycles trough all Voltages (pwm), waiting for a time (Tpwm), all corresponding to Freqs (i) from the above. Then when Finished time T is over or POS-ition 2 is reached it will do the Same thing once more.
The End.

can you take a short inspection and maybe give a critique for the Programming?

Thanks and best Wishes,
David

  1. put your global variables outside of setup() and loop()

and then I stop . your program is like Joey's French (in Friends)

Goenndalf:
can you take a short inspection and maybe give a critique for the Programming?

Does the program do what you want?

If not, tell us what it actually does and what you want it to do that is different.

...R

And please modify your post and use the code button </>

so your code looks like this

and is easy to copy to a text editor. See How to use the Forum

Function 'digitalread' could not be resolved  ATest.ino /ATest  line 8  Semantic Error

../ATest.ino: In function 'void setup()':
../ATest.ino:4:1: error: expected ',' or ';' before 'float'
 float T = 0.29       // Average Period (1/freqs)
 ^
../ATest.ino:11:1: error: expected initializer before '}' token
 }
 ^
../ATest.ino:2:7: warning: unused variable 'Freqs' [-Wunused-variable]
 float Freqs[] = {5, 4, 3, 2, 1, 2, 3, 4, 5};
       ^
../ATest.ino:3:7: warning: unused variable 'Freq' [-Wunused-variable]
 float Freq = 3.4      // Average frequency of the above
       ^
../ATest.ino:8:5: warning: unused variable 'POS1' [-Wunused-variable]
 int POS1 = digitalRead(A2);                // read analog pin A2 as digital and set it to a variable
     ^
../ATest.ino:9:5: warning: unused variable 'POS2' [-Wunused-variable]
 int POS2 = digitalRead(A0);
     ^
../ATest.ino: In function 'void loop()':
../ATest.ino:16:19: error: expected ';' before ')' token
 for (i = 0; i < 18) {
                   ^
../ATest.ino:17:18: error: 'freqs' was not declared in this scope
 float pwm = (255*freqs)/5
                  ^
../ATest.ino:19:3: error: expected ',' or ';' before '}' token
   }
   ^
../ATest.ino:17:7: warning: unused variable 'pwm' [-Wunused-variable]
 float pwm = (255*freqs)/5
       ^
../ATest.ino:20:6: error: 'POS1' was not declared in this scope
   if(POS1 == HIGH)  // time to start
      ^
../ATest.ino:22:1: error: 'lastUpdate' was not declared in this scope
 lastUpdate = millis();
 ^
../ATest.ino:23:16: error: 'pwm' was not declared in this scope
 analogWrite(3, pwm)
                ^
../ATest.ino:31:10: error: expected '(' before 'else'
       if else((POS2 == HIGH) || ((millis() - lastUpdate) > T))
          ^
../ATest.ino:45:30: error: expected '}' at end of input
 if j == 17{ {  while(1) { }; } // end programm.
                              ^
../ATest.ino:45:30: error: expected '}' at end of input

You need more of these ;
And more of these [code][/code]

  1. reformat your program (ctrl-T in the Arduino IDE) so it's a lot more readable.
  2. make sure it compiles (lots of ; missing at least, probably more of that).
  3. run it on your Arduino, and see what it does.
  4. if any specific questions: post code with description of what it does, and what it should do, and any relevant output you get.

Soo thanks for the Replies, its much appreciated!

 float Freqs[] = {5, 4, 3, 2, 1, 2, 3, 4, 5};
float Freq = 3.4      // Average frequency of the above
float T = 0.29       // Average Period (1/freqs)
pinMode(3, OUTPUT);
pinMode(A0, INPUT);
pinMode(A2, INPUT);
int POS1 = digitalread(A2);                // read analog pin A2 as digital and set it to a variable
int POS2 = digitalread(A0);
unsigned long lastUpdate

void setup() {
}

void loop()
{
  int i;
  for (i = 0; i < 10) {
    float pwm = (255 * freqs[i]) / 5
                float Tpwm = ((1 / Freq) * (Freq / Freq[i])
  }
                           if (POS1 == HIGH) // time to start
  {
    lastUpdate = millis();
    analogWrite(3, pwm)

    if ((millis() - lastUpdate) > Tpwm) // time to update
    {
      lastUpdate = millis();
      analogWrite(3, pwm);
      i = i + 1;
    }

    if else((POS2 == HIGH) || ((millis() - lastUpdate) > T))
      {
        int j;
        for (j = 0; j < 10) {
          float pwm2 = (255 * freqs[j]) / 5
                       float Tpwm2 = (1 / Freq) * (Freq / Freqs[j])

                               lastUpdate = millis();
          analogWrite(3, pwm2)

          if ((millis() - lastUpdate) > Tpwm2)
          { lastUpdate = millis();
            analogWrite(3, pwm2);
            j = j + 1;
          }
          if j == 17{ {  while

Edited and formatted.

Indeed I have not yet tested if it does what I want it to do. I will find myself an Oscilloscope and watch it.

bW,
David

ps. maybe you can tell wether or not I have used the for function correctly. I want pwm and Tpwm to change accordingly to ( i ), then it should change when its time has come.

  int i;
  for (i = 0; i < 10) {
    float pwm = (255 * freqs[i]) / 5
                float Tpwm = ((1 / Freq) * (Freq / Freq[i])
  }
                           if (POS1 == HIGH) // time to start
  {
    lastUpdate = millis();
    analogWrite(3, pwm)

    if ((millis() - lastUpdate) > Tpwm) // time to update
    {
      lastUpdate = millis();
      analogWrite(3, pwm);
      i = i + 1;
    }

output coming soon..

Indeed I have not yet tested if it does what I want it to do.

You haven't even tried to compile it yet.

Goenndalf:
Indeed I have not yet tested if it does what I want it to do. I will find myself an Oscilloscope and watch it.

Testing should be an integral part of program writing. Write a few lines - test, correct and get it working. Add a few more lines - test, correct etc.

Indeed the planning of a program should include planning for how to test it.

...R

Planning and Implementing a Program

AWOL:
Indeed I have not yet tested if it does what I want it to do. I will find myself an Oscilloscope and watch it.

And indeed you haven't even tried to compile it.

Not going to spend any time on this, just doesn't make sens.

Hello again!

Here I am dragging this old thread to daylight again. I hace since rewritten the Sketch, thereby shortening it, an have tested and pushed it over and over. This is the up to date sketch:

float Freqs[] = {0.4, 0.3, 0.2, 2, 3, 4};   // Voltage/Frequency Value Array
float DFreq = 2.2;        //Average of above
float AFreq = 11;        // Sum of Above
float T = (1000 / 2.2);       //Making of Period and converting to millis (multiply 1000)
unsigned long lastUpdate;
unsigned long START;
int i;
float pwm = (255 * Freqs[i] / 5);
float Tpwm = (T * Freqs[i] / AFreq);

void setup() {
  pinMode(9, OUTPUT);
}
void loop() {
  if (millis() == 5000) { // time to start
    START = millis();
    lastUpdate = millis();
    for (i = 0; i < 6; i = i + 1) {
      
      analogWrite(9, pwm);
      
      if ((millis() - lastUpdate) >= Tpwm) { // time to update
      i = i + 1;
      lastUpdate = millis();
       analogWrite(9, pwm);
       
       if ((millis() - START) == T) { // Repeat after one Period
           i = 0;}
          }
      }    
}
}

Soo, what I want it to do is this: put pwm on Pin 9, then when the time has come put updated pwm's on pin 9. Until the full Period is over, at this point it should restart.

What it does is the Following. It stays Quiet for the first 5000ms (as coded in the first loop-line), then it will output the first pwm (i), then stay there, forever!

My own critic would be the placement of the if sentences, should I put them infront or after analog write.
It seems as soon as little arduino starts his analogwriting he is busy until I put him out of comission again.

On the other hand there might be an easier way to do it, without using the for loop altogether?

Suggestion and critique welcome

David

Your use of a for loop, with code in the body that starts the loop over again, feels inappropriate.

       if ((millis() - START) == T) { // Repeat after one Period

Expecting the difference between two unsigned longs to exactly equal a float is not a realistic expectation.

Goenndalf:
Suggestion and critique welcome

The variable Tpwm should be unsigned long as that is what millis() returns.
The variable pwm should be byte as analogWrite() takes a value from 0 to 255

In general you should avoid float variables on an Arduino as floating point maths is very slow. Most things can be done with integer maths after a little thought.

I have no idea what this is meant to do

if ((millis() - START) == T) { // Repeat after one Period
           i = 0;}
          }

and, in any case the variable T should be unsigned long.

This line

float pwm = (255 * Freqs[i] / 5);

will be using the value 0 for i

...R

Goenndalf:

void loop() {

if (millis() == 5000) { // time to start

This will run exactly one time five seconds after power on (and only if it doesn't miss the 5000 mark - won't happen in this situation as you're not doing anything else).

If you want it to wait 5 seconds to start up, set a delay(5000) in your setup().

Thanks again to everybody!

it seems I have plenty things to learn still :slight_smile:

I have since changed to using the switch case method, which works like a charm. Its good to see where my trouble was at though..

David