Arduino Code Problem!

Hello, I am trying to write a code but I am failing. Can someone help me? What I want the code to do is to start a loop when I press the D15 button once and the loop will continue until I press the button again. When I press the D14 button while the loop is in progress, case6 will be removed from the loop and if I press the D14 button again, it will be included in the loop. The problem is that there is another loop in the existing code. It is a code that loops between A/B channels when the D25 button is pressed. The loop I want does not start unless I press D25. I want it to work independently of this.`

Blok alıntı

#include "TimerOne.h"
#include <SPI.h>
#include <MD_AD9833.h>
#include <TimerThree.h> // Yeni timer kütüphanesi eklendi

#define FASTADC 1
// defines for setting and clearing register bits
#ifndef cbi
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
#endif
#ifndef sbi
#define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
#endif

int buttonState = 0;
int i = 0;
int result = 6;
int M1 = 0;
int M2 = 0;
int M3 = 0;
int M4 = 0;
int M5 = 0;
int M6 = 1;

int MF1 = 1;
int MF2 = 0;
int resultF = 1;
int MFR1 = 1;
int MFR2 = 0;
int MFR3 = 0;
int resultFR = 1;

int t = 1;
long int analog;
long int ti = 500000;

int I = 0;
int te = 0;

int valueX[270];
int valueY[270];

const int CS = 48;
const int CSV = 49;
const int CSI = 46;
int value5 = 100;

// Yeni eklenen değişkenler (D15 ve D14 için)
const int BUTTON_D15 = 15; // D15 buton pini (case döngüsü)
const int BUTTON_D14 = 14; // D14 buton pini (case 6 kontrolü)
volatile bool cycleActive = false; // Case döngüsü durumu
volatile bool includeCase6 = true; // Case 6'nın döngüde olup olmadığını kontrol eder
volatile int currentCase = 1; // Şu anki case numarası

// MD_AD9833 pin definitions
const int FSYNC = 21; // FSYNC pin for AD9833
MD_AD9833 AD9833(FSYNC);

//---------------------------------Setup--------------------------------
void setup() {
Serial.begin(9600);
SPI.begin();

#if FASTADC
// set prescale to 16
sbi(ADCSRA, ADPS2);
cbi(ADCSRA, ADPS1);
cbi(ADCSRA, ADPS0);
#endif

for (int inc = 6; inc <= 13; inc++) {
pinMode(inc, OUTPUT);
}
for (int inc = 22; inc <= 44; inc = inc + 2) {
pinMode(inc, OUTPUT);
}
for (int inc = 23; inc <= 45; inc = inc + 2) {
pinMode(inc, INPUT);
}
pinMode(CS, OUTPUT);
pinMode(CSV, OUTPUT);
pinMode(CSI, OUTPUT);
pinMode(3, OUTPUT);
digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
digitalWrite(12, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);

digitalWrite(34, HIGH);
digitalWrite(42, HIGH);
digitalWrite(44, HIGH);
digitalWrite(40, HIGH);
digitalWrite(38, HIGH);
digitalWrite(36, HIGH);

// Yeni eklenen pin ayarları
pinMode(BUTTON_D15, INPUT_PULLUP);
pinMode(BUTTON_D14, INPUT_PULLUP);

// Initialize AD9833
AD9833.begin();
AD9833.setMode(MD_AD9833::MODE_SINE);

// Timer3'ü başlat (1 saniye aralık)
Timer3.initialize(1000000);
}

// Timer3 için callback fonksiyonu (case döngüsü)
void cycleCallback() {
if (cycleActive) {
int maxCase = includeCase6 ? 6 : 5; // Case 6 dahil mi kontrol et
if (currentCase < maxCase) {
currentCase++; // Bir sonraki case'e geç
} else {
currentCase = 1; // Başa dön
}
Serial.println("Current Case: " + String(currentCase)); // Test için
}
}

// Mevcut Timer1 callback fonksiyonu (A/B döngüsü)
void callback() {
if (t == 1) {
digitalWrite(34, HIGH);
digitalWrite(44, LOW);
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
t++;
} else {
digitalWrite(34, LOW);
digitalWrite(44, HIGH);
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
t = 1;
}
}

//-------------------------------------button---------------------------
int menubutton() {
for (i = 35; i <= 45; i = i + 2) {
if (digitalRead(i) == 1) {
if (i == 45) result = 6;
if (i == 43) result = 5;
if (i == 41) result = 4;
if (i == 39) result = 3;
if (i == 37) result = 2;
if (i == 35) result = 1;
}
}
if (digitalRead(33) == 1) { //A
resultF = 1;
}
if (digitalRead(23) == 1) { //B
resultF = 2;
}
if (digitalRead(25) == 1) { //A/B
resultF = 3;
}
//----------------Freq---------------
if (digitalRead(27) == 1) { //120
resultFR = 1;
}
if (digitalRead(29) == 1) { //1000
resultFR = 2;
}
if (digitalRead(31) == 1) { //2000
resultFR = 3;
}
return result;
}

//------------------------------------------------Sets the digital potentiometers-------------------------------------
void MCP41010Write(byte value) {
digitalWrite(CS, LOW);
SPI.transfer(B00010001); // This tells the chip to set the pot
SPI.transfer(value); // This tells it the pot position
digitalWrite(CS, HIGH);
}

//--------------------------------------------Sets the V pot----------------------------------------------------------
void MCP41010WriteV(byte valueV) {
digitalWrite(CSV, LOW);
SPI.transfer(B00010001); // This tells the chip to set the pot
SPI.transfer(valueV); // This tells it the pot position
digitalWrite(CSV, HIGH);
}

//--------------------------------------------Sets the I pot----------------------------------------------------------
void MCP41010WriteI(byte valueI) {
digitalWrite(CSI, LOW);
SPI.transfer(B00010001); // This tells the chip to set the pot
SPI.transfer(valueI); // This tells it the pot position
digitalWrite(CSI, HIGH);
}

// the loop function runs over and over again forever
void loop() {
menubutton();

// D15 butonu kontrolü (case döngüsünü başlat/durdur)
static bool lastD15State = LOW;
bool currentD15State = digitalRead(BUTTON_D15);
if (currentD15State == HIGH && lastD15State == LOW) { // Yükselen kenar algıla
if (!cycleActive) {
cycleActive = true;
currentCase = 1; // Döngü başlarken case 1'den başla
Timer3.attachInterrupt(cycleCallback); // Timer3'ü başlat
Serial.println("Case dongusu basladi");
} else {
cycleActive = false;
Timer3.detachInterrupt(); // Timer3'ü durdur
Serial.println("Case dongusu durdu");
}
}
lastD15State = currentD15State;

// D14 butonu kontrolü (case 6’yı dahil et/çıkar)
static bool lastD14State = LOW;
bool currentD14State = digitalRead(BUTTON_D14);
if (currentD14State == HIGH && lastD14State == LOW) { // Yükselen kenar algıla
includeCase6 = !includeCase6; // Case 6’yı toggle et
if (!includeCase6 && currentCase == 6) {
currentCase = 1; // Case 6 hariç tutulursa başa dön
}
Serial.println("Case 6 durumu: " + String(includeCase6));
}
lastD14State = currentD14State;

// Orijinal A/B döngüsü (Timer1 ile)
switch (resultF) {
case 1: //---------------------------A--------------------
if (MF1 == 1) {
detachInterrupt(callback);
cli();
digitalWrite(13, HIGH);
digitalWrite(12, LOW);
digitalWrite(34, HIGH);
digitalWrite(44, LOW);
digitalWrite(42, LOW);
MF1 = 0;
MF2 = 1;
}
break;
case 2: //---------------------------B--------------------
if (MF2 == 1) {
detachInterrupt(callback);
cli();
digitalWrite(13, LOW);
digitalWrite(12, HIGH);
digitalWrite(34, LOW);
digitalWrite(44, HIGH);
digitalWrite(42, LOW);
MF1 = 1;
MF2 = 0;
}
break;
case 3: //---------------------------A/B--------------------
sei();
digitalWrite(42, HIGH);
analog = analogRead(69); // A15 corresponds to digital pin 69
ti = (analog + 50) * 800;
Timer1.initialize(ti); // initialize timer1, and set a 1/2 second period
Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
MF1 = 1;
MF2 = 1;
break;
}
Serial.println(resultF);

// Orijinal frekans kontrolü
switch (resultFR) {
case 1: //---------------------------120Hz--------------------
if (MFR1 == 1) {
AD9833.setFrequency(MD_AD9833::CHAN_0, 50);
digitalWrite(36, LOW);
digitalWrite(38, LOW);
digitalWrite(40, HIGH);
MFR1 = 0;
MFR2 = 1;
MFR3 = 1;
}
break;
case 2: //---------------------------1000Hz--------------------
if (MFR2 == 1) {
AD9833.setFrequency(MD_AD9833::CHAN_0, 200);
digitalWrite(36, LOW);
digitalWrite(38, HIGH);
digitalWrite(40, LOW);
MFR1 = 1;
MFR2 = 0;
MFR3 = 1;
}
break;
case 3: //---------------------------2000Hz--------------------
if (MFR3 == 1) {
AD9833.setFrequency(MD_AD9833::CHAN_0, 2000);
digitalWrite(36, HIGH);
digitalWrite(38, LOW);
digitalWrite(40, LOW);
MFR1 = 1;
MFR2 = 1;
MFR3 = 0;
}
break;
}

// Orijinal case kontrolü
switch (result) {
case 1: //---------------------------VR1_VERY_LOW--------------------
if (M1 == 1) {
digitalWrite(32, HIGH); //------------------LEDS--------------------
digitalWrite(30, LOW);
digitalWrite(28, LOW);
digitalWrite(26, LOW);
digitalWrite(24, LOW);
digitalWrite(22, LOW);

    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, LOW);
    M1 = 0;
    M2 = 1;
    M3 = 1;
    M4 = 1;
    M5 = 1;
    M6 = 1;
    MCP41010WriteI(4);
    MCP41010Write(5);
    MCP41010WriteV(0);
  }
  break;

case 2: //---------------------------VR2_LOW--------------------
  if (M2 == 1) {
    digitalWrite(32, LOW); //------------------LEDS--------------------
    digitalWrite(30, HIGH);
    digitalWrite(28, LOW);
    digitalWrite(26, LOW);
    digitalWrite(24, LOW);
    digitalWrite(22, LOW);

    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, LOW);
    digitalWrite(11, HIGH);
    M1 = 1;
    M2 = 0;
    M3 = 1;
    M4 = 1;
    M5 = 1;
    M6 = 1;
    MCP41010WriteI(4);
    MCP41010Write(29);
    MCP41010WriteV(3);
  }
  break;

case 3: //---------------------------VR3_MED_1--------------------
  if (M3 == 1) {
    digitalWrite(32, LOW); //------------------LEDS--------------------
    digitalWrite(30, LOW);
    digitalWrite(28, HIGH);
    digitalWrite(26, LOW);
    digitalWrite(24, LOW);
    digitalWrite(22, LOW);

    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, LOW);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    M1 = 1;
    M2 = 1;
    M3 = 0;
    M4 = 1;
    M5 = 1;
    M6 = 1;
    MCP41010WriteI(1);
    MCP41010Write(52);
    MCP41010WriteV(6);
  }
  break;

case 4: //---------------------------VR4_MED_2--------------------
  if (M4 == 1) {
    digitalWrite(32, LOW); //------------------LEDS--------------------
    digitalWrite(30, LOW);
    digitalWrite(28, LOW);
    digitalWrite(26, HIGH);
    digitalWrite(24, LOW);
    digitalWrite(22, LOW);

    digitalWrite(6, HIGH);
    digitalWrite(7, HIGH);
    digitalWrite(8, LOW);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    M1 = 1;
    M2 = 1;
    M3 = 1;
    M4 = 0;
    M5 = 1;
    M6 = 1;
    MCP41010WriteI(1);
    MCP41010Write(110);
    MCP41010WriteV(12);
  }
  break;

case 5: //---------------------------VR5_HIGH--------------------
  if (M5 == 1) {
    digitalWrite(32, LOW); //------------------LEDS--------------------
    digitalWrite(30, LOW);
    digitalWrite(28, LOW);
    digitalWrite(26, LOW);
    digitalWrite(24, HIGH);
    digitalWrite(22, LOW);

    digitalWrite(6, HIGH);
    digitalWrite(7, LOW);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    M1 = 1;
    M2 = 1;
    M3 = 1;
    M4 = 1;
    M5 = 0;
    M6 = 1;
    MCP41010WriteI(1);
    MCP41010Write(189);
    MCP41010WriteV(22);
  }
  break;

case 6: //---------------------------VR6_VERY_HIGH--------------------
  if (M6 == 1) {
    digitalWrite(32, LOW); //------------------LEDS--------------------
    digitalWrite(30, LOW);
    digitalWrite(28, LOW);
    digitalWrite(26, LOW);
    digitalWrite(24, LOW);
    digitalWrite(22, HIGH);

    digitalWrite(6, LOW);
    digitalWrite(7, HIGH);
    digitalWrite(8, HIGH);
    digitalWrite(9, HIGH);
    digitalWrite(10, HIGH);
    digitalWrite(11, HIGH);
    M1 = 1;
    M2 = 1;
    M3 = 1;
    M4 = 1;
    M5 = 1;
    M6 = 0;
    MCP41010WriteI(1);
    MCP41010Write(254);
    MCP41010WriteV(30);
  }
  break;

}

// Yeni case döngüsü (Timer3 ile, orijinal döngüden bağımsız)
if (cycleActive) {
switch (currentCase) {
case 1:
digitalWrite(32, HIGH);
digitalWrite(30, LOW);
digitalWrite(28, LOW);
digitalWrite(26, LOW);
digitalWrite(24, LOW);
digitalWrite(22, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
MCP41010WriteI(4);
MCP41010Write(5);
MCP41010WriteV(0);
break;
case 2:
digitalWrite(32, LOW);
digitalWrite(30, HIGH);
digitalWrite(28, LOW);
digitalWrite(26, LOW);
digitalWrite(24, LOW);
digitalWrite(22, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, HIGH);
MCP41010WriteI(4);
MCP41010Write(29);
MCP41010WriteV(3);
break;
case 3:
digitalWrite(32, LOW);
digitalWrite(30, LOW);
digitalWrite(28, HIGH);
digitalWrite(26, LOW);
digitalWrite(24, LOW);
digitalWrite(22, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
MCP41010WriteI(1);
MCP41010Write(52);
MCP41010WriteV(6);
break;
case 4:
digitalWrite(32, LOW);
digitalWrite(30, LOW);
digitalWrite(28, LOW);
digitalWrite(26, HIGH);
digitalWrite(24, LOW);
digitalWrite(22, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
MCP41010WriteI(1);
MCP41010Write(110);
MCP41010WriteV(12);
break;
case 5:
digitalWrite(32, LOW);
digitalWrite(30, LOW);
digitalWrite(28, LOW);
digitalWrite(26, LOW);
digitalWrite(24, HIGH);
digitalWrite(22, LOW);
digitalWrite(6, HIGH);
digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
MCP41010WriteI(1);
MCP41010Write(189);
MCP41010WriteV(22);
break;
case 6:
if (includeCase6) {
digitalWrite(32, LOW);
digitalWrite(30, LOW);
digitalWrite(28, LOW);
digitalWrite(26, LOW);
digitalWrite(24, LOW);
digitalWrite(22, HIGH);
digitalWrite(6, LOW);
digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
MCP41010WriteI(1);
MCP41010Write(254);
MCP41010WriteV(30);
}
break;
}
}
Mega AD9833-Cycle Mode.ino (16.0 KB)

Please, take a look at:

Then, fix your first post, editing it so all of the code is within the code tags. That allows helpers to capture your code and import it into their IDE, where they can work on fixing it up for you.

Finally, search on this forum for tutorials about doing many things at once. Your code is a prime candidate for such a mechanism.
Thanks

1 Like

look this over

  • it simply demonstrates how to structure code to do what i think you want
  • you need to change the pin #s for the buttons
const byte PinButs [] = { A2, A1, A3 };
const int  Nbut       = sizeof(PinButs);

byte       butState [Nbut];

bool       enabled  [Nbut];

// -----------------------------------------------------------------------------
bool
isPressed (
    int  butIdx )
{
    byte but = digitalRead (PinButs [butIdx]);
    if (butState [butIdx] != but)  {    // state change
        butState [butIdx]  = but;
        delay (20);                     // debounce
        if (LOW == but)
            return true;                // pressed
    }
    return false;
}

// -----------------------------------------------------------------------------
void
loop (void)
{
    // monitor buttons
    for (int n = 0; n < Nbut; n++)  {
        if (isPressed (n))
            enabled [n] = ! enabled [n];
    }

    // service loops
    if (enabled [1])  {
        Serial.println ("                loop D15");

        if (enabled [0])
            Serial.println ("sub-loop D14");
    }
    
    if (enabled [2])
        Serial.println ("                               loop D25");

}

void
setup (void)
{
    Serial.begin (9600);
}

This Topic has been locked as it is the identical or too similar your other topic.

Please do not duplicate your questions as doing so wastes the time and effort of the volunteers trying to help you as they are then answering the same thing in different places.

Please create one topic only for your question and choose the forum category carefully. If you have multiple questions about the same project then please ask your questions in the one topic as the answers to one question provide useful context for the others, and also you won’t have to keep explaining your project repeatedly.

Repeated duplicate posting could result in a temporary or permanent ban from the forum.

Could you take a few moments to Learn How To Use The Forum

It will help you get the best out of the forum in the future.

Thank you.