K changes loop not working

Hi In first program #1 buttons are inside void loop, I trying to create seperate loop for them = program #2 but it is not working.
#1

int k;
void setup() {
  Serial.begin(115200);
  pinMode(32, INPUT_PULLUP);    //k++
  pinMode(33, INPUT_PULLUP);   //k--
 }

void loop()
{
if (digitalRead(32) == LOW)
    {
    k++;
    }
    if (digitalRead(33) == LOW)
    {
    k--;
    }
    Serial.print( "k   = ");    
    Serial.println(k);
    delay( 100 );
 }

#2

int k;
void setup() {
  Serial.begin(115200);
  pinMode(32, INPUT_PULLUP);    //k++
  pinMode(33, INPUT_PULLUP);   //k--
 }

void loop() {
  // put your main code here, to run repeatedly:

}
void k_changes()
{
  if (digitalRead(32) == LOW)
    {
    k++;
    }
    if (digitalRead(33) == LOW)
    {
    k--;
    }
    Serial.print( "k   = ");    
    Serial.println(k);
    delay( 100 );
}

The setup() and loop() subroutines are special cases - it called automatically. Any other subroutines in the code MUST be called explicitly whether on setup or loop.

I don't found where you call your k_changes() subroutine in the code.

Because I don't know how to do it and that I am here.

For example

I recommend you to read a few first chapters of any C++ (Arduino) programming book before going further.

I did your suggestion, but I have an error

K_changes:9:7: error: ambiguating new declaration of 'int k_changes()'
    9 |   int k_changes();
      |       ^~~~~~~~~
C:\Users\Galinka\Documents\Arduino\K_changes\K_changes.ino:12:6: note: old declaration 'void k_changes()'
   12 | void k_changes()
      |      ^~~~~~~~~
exit status 1
ambiguating new declaration of 'int k_changes()'

...because you did it wrong.

Where did you seen an 'int' before function call in my post ? Do you know what the void and int means before function name?

I strictly recommend you read the textbook...

This is what I did

int k;

void setup() {
  Serial.begin(115200);
  pinMode(32, INPUT_PULLUP);    //k++
  pinMode(33, INPUT_PULLUP);   //k--
}

void loop() {
  k_changes();

}
void k_changes()
{
  if (digitalRead(32) == LOW)
  {
    k++;
  }
  if (digitalRead(33) == LOW)
  {
    k--;
  }
  Serial.print( "k   = ");
  Serial.println(k);
  delay( 100 );
}

If you had done so, there would not have been such a error:

1 Like

No, I really don't think it was.

Compiling this

k_changes.ino

int k;

void setup() {
  Serial.begin(115200);
  pinMode(32, INPUT_PULLUP);    //k++
  pinMode(33, INPUT_PULLUP);   //k--
}

void loop() {
  k_changes();

}
void k_changes()
{
  if (digitalRead(32) == LOW)
  {
    k++;
  }
  if (digitalRead(33) == LOW)
  {
    k--;
  }
  Serial.print( "k   = ");
  Serial.println(k);
  delay( 100 );
}

Produces this

arduino-cli compile -b arduino:avr:mega:cpu=atmega2560 --warnings all --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Mega/test)
Sketch uses 5830 bytes (2%) of program storage space. Maximum is 253952 bytes.
Global variables use 196 bytes (2%) of dynamic memory, leaving 7996 bytes for local variables. Maximum is 8192 bytes.
Compilation finished successfully.

which indicates success.

Anything look just a little suspicious possibly to anybody?

1 Like

Thanks it is working

Me, I'm wondering just what the extension (if any) on that file is.

Anyhow, I've found it to compile just fine for me here, so that's the extent of my interest in the matter.

Characters next to the file name usually indicate "edits not saved"

1 Like

Ah, thanks for that. I haven't used the IDE in years. That would certainly do it.

and since IDE usually automatically saves the sketch before compiling... then such a picture means that OP first compiled the wrong code (to show the errors?) , and then corrected it and made a screenshot.

And I'm at a loss, was this done by accident or on purpose?

Check his most recent thread:
https://forum.arduino.cc/t/void-loop-problem/1391775

Which is probably related.

Hi
Adding anything inside the void loop causes the sine wave to be distorted . This is a sin wave generator program with addet - k_changes();, but adding a simple blinking LED also affects the shape of the sine wave.

#include "driver/i2s.h"

#define SAMPLE_RATE 400000
#define WAVEFORM_SAMPLES 256

uint8_t stereo_buffer[WAVEFORM_SAMPLES * 2];
uint8_t wave_data1[WAVEFORM_SAMPLES];
uint8_t wave_data2[WAVEFORM_SAMPLES];
int k;
void generateWaveforms() {
  for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
    wave_data1[i] = (uint8_t)((255 * i) / WAVEFORM_SAMPLES);
    wave_data2[i] = (uint8_t)(127.5 * (1 + sin(2 * 3.14159 * i / WAVEFORM_SAMPLES)));
  }
  for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
    stereo_buffer[i * 2] = wave_data1[i];      // DAC1
    stereo_buffer[i * 2 + 1] = wave_data2[i];  // DAC2
  }
}

void setup() {
  Serial.begin(115200);
  generateWaveforms();

  i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
    .sample_rate = SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_8BIT,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = 0,
    .dma_buf_count = 8,
    .dma_buf_len = WAVEFORM_SAMPLES * 2,
    .use_apll = false,
  };

  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_pin(I2S_NUM_0, NULL);
  i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  i2s_set_sample_rates(I2S_NUM_0, i2s_config.sample_rate);
}

void loop() {
  size_t bytes_written;
  i2s_write(I2S_NUM_0, stereo_buffer, sizeof(stereo_buffer), &bytes_written, portMAX_DELAY);
  k_changes();

}
//+++++++++++++++++++++++++++++++++++++++++++++++++
void k_changes()
{
  if (digitalRead(32) == LOW)
  {
    k++;
  }
  if (digitalRead(33) == LOW)
  {
    k--;
  }
  Serial.print( "k   = ");
  Serial.println(k);
  delay( 100 );
}

Post the sketch with the added blinking LED

Did you expect anything other while adding a delay to the loop?

#include "driver/i2s.h"

#define SAMPLE_RATE 400000
#define WAVEFORM_SAMPLES 256

uint8_t stereo_buffer[WAVEFORM_SAMPLES * 2];
uint8_t wave_data1[WAVEFORM_SAMPLES];
uint8_t wave_data2[WAVEFORM_SAMPLES];
int k;
int LED_13;
void generateWaveforms() {
  for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
    wave_data1[i] = (uint8_t)((255 * i) / WAVEFORM_SAMPLES);
    wave_data2[i] = (uint8_t)(127.5 * (1 + sin(2 * 3.14159 * i / WAVEFORM_SAMPLES)));
  }
  for (int i = 0; i < WAVEFORM_SAMPLES; i++) {
    stereo_buffer[i * 2] = wave_data1[i];      // DAC1
    stereo_buffer[i * 2 + 1] = wave_data2[i];  // DAC2
  }
}

void setup() {
  Serial.begin(115200);
  pinMode(LED_13, OUTPUT);
  generateWaveforms();

  i2s_config_t i2s_config = {
    .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN),
    .sample_rate = SAMPLE_RATE,
    .bits_per_sample = I2S_BITS_PER_SAMPLE_8BIT,
    .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
    .communication_format = I2S_COMM_FORMAT_I2S_MSB,
    .intr_alloc_flags = 0,
    .dma_buf_count = 8,
    .dma_buf_len = WAVEFORM_SAMPLES * 2,
    .use_apll = false,
  };

  i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
  i2s_set_pin(I2S_NUM_0, NULL);
  i2s_set_dac_mode(I2S_DAC_CHANNEL_BOTH_EN);
  i2s_set_sample_rates(I2S_NUM_0, i2s_config.sample_rate);
}

void loop() {
  size_t bytes_written;
  i2s_write(I2S_NUM_0, stereo_buffer, sizeof(stereo_buffer), &bytes_written, portMAX_DELAY);
  // k_changes();
  ////////////////////////////////
  digitalWrite(LED_13, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);                       // wait for a second
  digitalWrite(LED_13, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);                       // wait for a second
  //////////////////////////////

}
//+++++++++++++++++++++++++++++++++++++++++++++++++
/*
  void k_changes()
  {
  if (digitalRead(32) == LOW)
  {
    k++;
  }
  if (digitalRead(33) == LOW)
  {
    k--;
  }
  Serial.print( "k   = ");
  Serial.println(k);
  delay( 100 );
  }
*/