Arduin Code Problem

#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;
    }
  }
}

Care to elaborate?

Hello everyone, I wanted to re-edit the topic upon the warning and open it. The problem is that I am working on a circuit and what I want to do is to simulate 6 buttons pressed with one second intervals. When the button connected to the D15 pin is pressed once, the cycle starts and when it is pressed again, the cycle stops. When the button connected to the D14 pin is pressed once when the cycle starts, 'case6' is included in the cycle and when the button is pressed once more, it is removed from the cycle. But there is a loop in the code, although it does not look exactly like this and it overlaps with it. This loop switches between A/B channels with the pot in the time set when the button connected to the D25 pin is pressed. The code I wrote should work independently of this but I could not do it. The code I wrote does not work without pressing the D25 button, that is, I have to first press the D25 button and run the A/B loop, then I can start the loop between the 'cases' with the D15 button. Thanks in advance for your help.

Your words don't add up to a description of the desired behaviour that I can make sense of.

Perhaps you could express it differently, not words! but pseudocode or a flowchart, whatever.

If your code was working and the documentation lost, I might be willing to use it as an operating specification with the goal of reproducing said documatation.

I'm sure that means something, and prolly we in the same room with a whiteboard, or even a backboard, you could help us make better sense of it. As it is, nearly pure confusion.

Thanks for putting the code in tags this time. It seems quite verbose and wou,d invite better attention if it were just a bit more succinct.

Arrays might help, as woukd functions hiding lines and line of output to numbered pins.

a7

1 Like

First of all, the reason why I couldn't put the code in the labels was related to the browser, I realized it too late. What I mean by loop is a kind of button simulation, that is, when I press a button, it simulates the other 6 buttons.

The code controls 6 relays, these start with ''VR1_VERY_LOW'' and continue until ''VR6_VERY_HIGH'', each one is selected with a button, what I want to do is to add 2 more buttons and simulate the first button and 6 buttons with a one-second interval, until the button is pressed again. While simulating with the second button, when the second button is pressed once, the sixth button is taken out of this process and when the button is pressed again, it is included in the process again. The problem is that there is something similar in the code, there are two separate relays, these relays are controlled by 3 buttons, the 1st button opens the 1st relay and closes the 2nd relay, the 2nd button opens the 2nd relay and closes the 1st relay. The 3rd button constantly opens and closes between the two relays. The simulation process I want to do does not start before the process here starts..

Try again.

You have by my count 8 buttons.

Six are not real, but are to be "pressed" by your tester. Name those, how about A, B, C and so forth. To F.

Two are real, name those, how about X and Y.

Now say how real buttons X and Y are to control (stopping, starting and, I think, whether to include "button" F or not) whether A-F are "pressed".

Do I correctly read that A-E or A-F are to be sequentially pressed, as in A down and up, B down and up cycling through (with or without) F? At a rate, one "press" per second?

Does restarting begin at A or where it left off?

I assume stopping should not shorten an A-F "press". How long should each press be?

Bothing you are asking for is very hard, but you've yet to 'splain this adequately. It is possible someone else is a better mind reader or has a crystal ball that isn't in the dishwasher ATM.

a7

1 Like

Thank you for your interest but I don't understand what you didn't understand, the buttons I mentioned as one and two are buttons I added later, connected to D14 and D15.When I press the D15 button once, I want to open them one by one, one second apart and continuously, if I press the D15 button once more, this loop will stop. Also, if I press the D14 button once while this process is in progress (int M6 = 1;), it will be removed from this process and if I press the D14 button again, it will be put back into process. Actually, this is what I want to do.
int M1 = 0;
int M2 = 0;
int M3 = 0;
int M4 = 0;
int M5 = 0;
int M6 = 1;

Be aware that cli() is going to disrupt timing functions (like millis(), micros(), and delay() functions). Even serial.print() should be affected...
Are you sure you need it?

It must be me, then. Good luck!

a7

Post a state-transition-diagram to see what happens under what conditions.

Sorry I had to edit your post a bit.
Please spread you post content outy so it is almost in point form.
This makes it easier to read.

Look at the structure of the replies.

Tom.... :smiley: :+1: :coffee: :australia:

The reason you have to press D25 first is that by pressing that button, you set resultF = 3

  if (digitalRead(25) == 1) {  //A/B
    resultF = 3;
  }

which later does this

    case 3:  //---------------------------A/B--------------------
      sei();

which enables interrupts. Your other cases turn off interrupts with cli() so your Timer3 interrupt never happens.

You should never be turning off interrupts and leaving them off.

1 Like