Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/arduino/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
For loop 我的用于回路的Arduino波形发生器赢得';t循环_For Loop_Arduino_Comparator_Wave - Fatal编程技术网

For loop 我的用于回路的Arduino波形发生器赢得';t循环

For loop 我的用于回路的Arduino波形发生器赢得';t循环,for-loop,arduino,comparator,wave,For Loop,Arduino,Comparator,Wave,我是一个新的程序员,我有一些关于我为Arduino编写的程序的问题 以下是我试图做的: 使用一对相互触发的for循环创建一个8位三角波发生器 控制波的增量/减量之间的时间,以管理频率 将一个8位变量threshold1与波的振幅进行比较,并在匹配时翻转led 我遇到了两个问题: 除非我注释掉逻辑的后半部分,否则两个for循环中的递增/递减条件都不会触发,如下所示 环路前缘和环路下降沿触发一次并停止,无需重新触发。我不明白这是为什么 我试着联系一些比我更有经验的朋友,但他们都被难倒了。我开始使

我是一个新的程序员,我有一些关于我为Arduino编写的程序的问题

以下是我试图做的:

  • 使用一对相互触发的for循环创建一个8位三角波发生器
  • 控制波的增量/减量之间的时间,以管理频率
  • 将一个8位变量
    threshold1
    与波的振幅进行比较,并在匹配时翻转led
我遇到了两个问题:

  • 除非我注释掉逻辑的后半部分,否则两个for循环中的递增/递减条件都不会触发,如下所示

  • 环路前缘和环路下降沿触发一次并停止,无需重新触发。我不明白这是为什么

  • 我试着联系一些比我更有经验的朋友,但他们都被难倒了。我开始使用串行监视器进行调试,这使我成功地从波形发生器中获得了一个周期,并使触发器工作(如果我注释掉逻辑条件的后半部分,如下所述)

    我希望了解的是如何更改代码,以便for循环在一个完整周期后按预期重新触发,以及解决多个条件(2个或更多比较)的增量/减量的最佳方法是什么

    这是我的密码:

    /* this program is my first attemp to design a dual window comparator within the arduino for use in a eurorack environment.
    / the basic premise is a 8bit triangle waveform generator and two 8bit variables. when the wave and variable match a
    / flip occurs, so 4 flips per period, on off on off, and when those occur (and therefore, the duty cycle of the pwn square
    / output) is controlled by the 2 threshold variables */
    
    // constants won't change. Used here to set a pin number:
    const int ledPin = 13;// the number of the LED pin
    
    // Variables will change:
    int ledState = LOW;// ledState used to set the LED
    long stpLngth = 200; //triangle wave step length TODO make an analog read do this and make a scaler to make it exponential control ei v/Oct
    
    void setup() {
      // set the digital pin as output:
      pinMode(ledPin, OUTPUT);
    
      // Time comparison for setting the wave frequency:
      unsigned long currentMillis = 0;
      unsigned long previousMillis = 0;
    
      byte threshold1 = 0b01100000; //starting position 98/256
      byte threshold2 = 0b10000000; //starting position 128/256
      // I will add code to handle a second threshold once I get the for loop to work as expected
    }
    
    void loop() {
      Serial.begin(9600);
      unsigned long currentMillis = millis();
      unsigned long previousMillis = 0;
      unsigned char wavePosition = 0;
      byte threshold1 = 0b01100000; //starting position 98/256
      byte threshold2 = 0b10000000; //starting position 128/256  
      long stepLength = 200;
      //this variable is to compare to the difference between currentMillis and previousMillis, and control frequency. TODO write a tap tempo and a mapped analog control for this
      /*This is the leading edge of my triangle wave, I want it to initiate a waveat 0 and increment if:
       * a) it's less than 254 and 
       * b) (currentMillis - previousMillis) is equal or greater than stepLength*/
    
      //I've commented out the extra logic conditions I was trying to use to control the frequency below and now it increments, but I am still hoping to learn a way to control the time between steps:
      for(wavePosition=0x00; wavePosition<0xff  /*&& ((currentMillis - previousMillis) >= stepLength)*/; wavePosition++) {
        previousMillis = currentMillis;
        Serial.println("increment waveform");
        //this is for debugging it prints each time the wave decrements
        if (threshold1 == wavePosition) {
          //compares threshold1 to wavePosition flipflop led on match
          if (ledState == LOW) {
            // if the LED is off turn it on and vice-versa:
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
          Serial.println("Led flip 2");
          //this is for debugging it prints when the led changes state
          digitalWrite(ledPin, ledState);
        }
      }
    
      // falling edge of wave same logic conditions commented out, this one decrements but at the end of the waveform it doesn't restart the rising edge for loop it just hangs.
      for(wavePosition=0xff; wavePosition>0x01/* && ((currentMillis - previousMillis >= stepLength)*/; wavePosition--) {
        previousMillis = currentMillis;
        Serial.println("decrement waveform");
        //this is for debugging it prints each time the wave decrements
        if (threshold1 == wavePosition) {
          //compares threshold1 to wavePosition flipflop led on match
          if (ledState == LOW) {
            // if the LED is off turn it on and vice-versa:
            ledState = HIGH;
          } else {
            ledState = LOW;
          }
          Serial.println("Led flip 2");
          //this is for debugging it prints when the led changes state
          digitalWrite(ledPin, ledState);
        }
      }
    }
    
    /*这个程序是我第一次尝试在arduino中设计一个双窗口比较器,用于eurorack环境。
    /基本前提是一个8位三角形波形发生器和两个8位变量。当波形和变量匹配
    /发生翻转,因此每个周期有4次翻转,打开-关闭-关闭,以及当这些翻转发生时(因此,pwn平方的占空比
    /输出)由2个阈值变量控制*/
    //常数不会改变。此处用于设置pin码:
    常数int ledPin=13;//LED引脚的编号
    //变量将发生变化:
    int-ledState=低;//用于设置LED的LED状态
    长stpLngth=200//三角波步长做一个模拟读数做这个,做一个定标器,使其指数控制EIV/Oct
    无效设置(){
    //将数字管脚设置为输出:
    引脚模式(LED引脚,输出);
    //设置波频率的时间比较:
    无符号长电流毫秒=0;
    无符号long-previousMillis=0;
    字节阈值1=0b01100000;//起始位置98/256
    字节阈值2=0b10000000;//起始位置128/256
    //一旦for循环按预期工作,我将添加代码来处理第二个阈值
    }
    void循环(){
    Serial.begin(9600);
    无符号长电流毫秒=毫秒();
    无符号long-previousMillis=0;
    无符号字符位置=0;
    字节阈值1=0b01100000;//起始位置98/256
    字节阈值2=0b10000000;//起始位置128/256
    长步长=200;
    //此变量用于比较currentMillis和previousMillis之间的差异,以及控制频率。要为此写入抽头节拍和映射的模拟控制
    /*这是三角形波的前沿,我希望它在0处开始一个波,如果:
    *a)小于254,并且
    *b)(当前毫秒-以前的毫秒)等于或大于步长*/
    //我已经注释掉了我试图用来控制以下频率的额外逻辑条件,现在它增加了,但我仍然希望学习一种控制步骤之间时间的方法:
    用于(wavePosition=0x00;wavePosition=stepLength)*/;wavePosition++){
    前一毫秒=当前毫秒;
    Serial.println(“增量波形”);
    //这是为了调试它在每次波衰减时打印
    if(阈值1==波位置){
    //将阈值1与匹配时的wavePosition触发器led进行比较
    如果(ledState==低){
    //如果LED关闭,则将其打开,反之亦然:
    ledState=高;
    }否则{
    ledState=低;
    }
    串行打印LN(“Led翻转2”);
    //这是用于调试led改变状态时打印的
    数字写入(ledPin、ledState);
    }
    }
    //波的下降沿相同的逻辑条件被注释掉,这一个递减,但在波形结束时,它不会重新启动上升沿,因为它只是挂起。
    对于(波位置=0xff;波位置>0x01/*&&((当前毫秒-先前毫秒>=步长)*/;波位置--){
    前一毫秒=当前毫秒;
    串行.println(“减量波形”);
    //这是为了调试它在每次波衰减时打印
    if(阈值1==波位置){
    //将阈值1与匹配时的wavePosition触发器led进行比较
    如果(ledState==低){
    //如果LED关闭,则将其打开,反之亦然:
    ledState=高;
    }否则{
    ledState=低;
    }
    串行打印LN(“Led翻转2”);
    //这是用于调试led改变状态时打印的
    数字写入(ledPin、ledState);
    }
    }
    }
    
    代码中包含很多bug(这是有道理的,因为你说你是一个初学者,请坚持下去)。我建议的最大问题是:

  • 作为初学者,在
    setup()
    loop()
    之前声明所有变量,以后不要重新声明它们
  • 将所有初始化代码移到
    setup()
  • 确保
    loop()
    不包含任何设置代码
  • 请记住,当您的Arduino草图运行时,Arduino将只调用一次
    setup()
    ,然后它将一次又一次地调用
    loop()

    例如,正如尼特里德指出的,
    Serial.begin(9600)
    应该在
    setup()
    内,而不是
    loop()
    内。此外,我看到
    threshold1
    threshold2
    都在
    setup()
    loop()
    中设置和声明。相反,它们应该在
    setup()之前声明
    循环()
    并在
    设置()中设置