0
votes
void setup() {
  // 
SysEx Config Message Structure:
0xF0 # SysEx message start byte
0x14 # Manufacturer ID; 0x14 is actually Fairlight, but I don't forsee too many conflicts here... 
0x01 # Knobber knob channel number
0x01 # Knobber button channel number
0x0E # Knobber knob CC number
0x0F # Knobber button CC number
0x01 # Knobber button behaviour (0 = momentary, 1 = toggle)
0xF7 # SysEx message end byte

*/

#include <Bounce.h>
#include <EEPROM.h>

// Default settings - will be overwritten if EEPROM values are present.
int knobChan = 1; int buttonChan = 1; int knobCC = 14; int buttonCC = 15; 
int kPin = 0; int bPin = 0; int behaviour = 1;
int inputAnalog, ccValue, iAlag;
boolean toggled = false;
Bounce button0 = Bounce(0,5);

void setup() {
  //MIDI rate
  Serial.begin(31250);
  pinMode(bPin, INPUT_PULLUP);
  delay(5);
  knobChan =  EEPROM.read(1); 
  usbMIDI.sendControlChange(44, knobChan, 2);
  delay(5);
  buttonChan = EEPROM.read(2); 
  delay(5);
  knobCC = EEPROM.read(3);    
  delay(5);
  buttonCC = EEPROM.read(4);
  delay(5);
  behaviour = EEPROM.read(5);
}

void loop() {
  // put your main code here, to run repeatedly:
  // Check for SysEx config message
  if(usbMIDI.read() && usbMIDI.getType() == 7) {                
     if (usbMIDI.getData1() > 1 && usbMIDI.getData1() < 9) {
        // unpack SysEx
        byte * sysbytes = usbMIDI.getSysExArray();
        if (sysbytes[0] == 0xf0 && sysbytes[7] == 0xf7) { // Good length; legit sysex.
          if(sysbytes[1] == 0x14) {  // It's either Knobber or a Fairlight CMI...
              // 2, 3, 4, 5 and 6 can now be written to EEPROM and to global vars
              EEPROM.write(1, sysbytes[2]);
              knobChan = sysbytes[2];
              EEPROM.write(2, sysbytes[3]);
              buttonChan = sysbytes[3];
              EEPROM.write(3, sysbytes[4]);
              knobCC = sysbytes[4];
              EEPROM.write(4, sysbytes[5]);
              buttonCC = sysbytes[5];
              EEPROM.write(5, sysbytes[6]);
              behaviour = sysbytes[6];

          }          
        }
     } 
 }


  if(behaviour > 0) {
      // Pushbutton - MOMENTARY behaviour
      button0.update();
      if (button0.fallingEdge()) {
          usbMIDI.sendControlChange(buttonCC, 127, buttonChan);
      }
      if (button0.risingEdge()) {
          usbMIDI.sendControlChange(buttonCC, 0, buttonChan);
      } 
  } else {      
      // Pushbutton - TOGGLE behaviour
      button0.update();
      if(button0.fallingEdge()) {
         if (toggled) {
             usbMIDI.sendControlChange(buttonCC, 0, buttonChan);
             toggled = false;
         } else {
             usbMIDI.sendControlChange(buttonCC, 127, buttonChan);
            toggled = true;
         } 
      }
  }

  inputAnalog = analogRead(kPin);  
  if(abs(inputAnalog - iAlag) > 7) {  
    // calc the CC value based on the raw value
    ccValue = inputAnalog/8;                                
    // Invert the pot value (because I soldered it backwards...)
    int inverted = map(ccValue, 127, 0, 0, 127);            
    // send the MIDI
    usbMIDI.sendControlChange(knobCC, inverted, knobChan);                                  
    iAlag = inputAnalog;
  }

  delay(5); // limits message frequency
}

I'm getting an error in the void loop block of code, I don't know what's exactly wrong and why is it giving me the following error: a function-definition is not allowed here before '{' token

It's my first time attempting this code so I don't know what is wrong

Please help as it is important for my class. I would really really appreciate it! :)

UPDATE: Here are the error messages that I'm getting:

Arduino: 1.8.10 (Mac OS X), Board: "Teensy 3.2 / 3.1, Serial, 96 MHz (overclock), Faster, US English"

In file included from /Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:28:0: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/EEPROM/EEPROM.h: In function 'void setup()':

/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/EEPROM/EEPROM.h:136:5: error: invalid declaration of member template in local class template< typename T > T &get( int idx, T &t ){ ^

/Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/EEPROM/EEPROM.h:143:5: error: invalid declaration of member template in local class template< typename T > const T &put( int idx, const T &t ){
^

Test1KnobberCode:37:14: error: a function-definition is not allowed here before '{' token void setup() { ^

Test1KnobberCode:54:13: error: a function-definition is not allowed here before '{' token void loop() { ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:31:5: warning: unused variable 'knobChan' [-Wunused-variable] int knobChan = 1; int buttonChan = 1; int knobCC = 14; int buttonCC = 15; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:31:23: warning: unused variable 'buttonChan' [-Wunused-variable] int knobChan = 1; int buttonChan = 1; int knobCC = 14; int buttonCC = 15; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:31:43: warning: unused variable 'knobCC' [-Wunused-variable] int knobChan = 1; int buttonChan = 1; int knobCC = 14; int buttonCC = 15; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:31:60: warning: unused variable 'buttonCC' [-Wunused-variable] int knobChan = 1; int buttonChan = 1; int knobCC = 14; int buttonCC = 15; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:32:5: warning: unused variable 'kPin' [-Wunused-variable] int kPin = 0; int bPin = 0; int behaviour = 1; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:32:19: warning: unused variable 'bPin' [-Wunused-variable] int kPin = 0; int bPin = 0; int behaviour = 1; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:32:33: warning: unused variable 'behaviour' [-Wunused-variable] int kPin = 0; int bPin = 0; int behaviour = 1; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:33:5: warning: unused variable 'inputAnalog' [-Wunused-variable] int inputAnalog, ccValue, iAlag; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:33:18: warning: unused variable 'ccValue' [-Wunused-variable] int inputAnalog, ccValue, iAlag; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:33:27: warning: unused variable 'iAlag' [-Wunused-variable] int inputAnalog, ccValue, iAlag; ^

/Users/bharatvangani/Documents/Arduino/Test1KnobberCode/Test1KnobberCode.ino:34:9: warning: unused variable 'toggled' [-Wunused-variable] boolean toggled = false; ^

Test1KnobberCode:116:1: error: expected '}' at end of input } ^

Multiple libraries were found for "Bounce.h" Used: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/Bounce Multiple libraries were found for "EEPROM.h" Used: /Applications/Arduino.app/Contents/Java/hardware/teensy/avr/libraries/EEPROM exit status 1 a function-definition is not allowed here before '{' token

1

1 Answers

0
votes

Your problem are the first two lines of your file. In the first line you start defining your setup function, in the second line you start a line comment, followed by some text. This should rather be a block comment.

To fix your issue, remove the void setup() { and change the line comment to the start of a block comment /*.

Update: Your error message output clearly supports my answer. Look at this simplified error:

In file included from Test1KnobberCode.ino: EEPROM.h: In function 'void setup()':

So here the compiler tells us that there is an error in the EEPROM library (which is very unlikely, so this is the first hint that we probably fucked up something ourselfs)

EEPROM.h: error: invalid declaration of member template in local class template< typename T > T &get( int idx, T &t ){

This is the exact error. Looks like somethings wrong with the declaration of a template class. The exact error likely does not matter, because the problem is on our side.

Test1KnobberCode: error: a function-definition is not allowed here before '{' token void setup() {

Another hint that we fucked up. Here the compiler tells us, that we can't define void setup() here (because we are currently in the scope of a function)

Test1KnobberCode: error: a function-definition is not allowed here before '{' token void loop() {

Same hint as before, just with void loop()

So to clarify my anser: Change this (the first 16 lines of your code)

void setup() {
  // 
SysEx Config Message Structure:
0xF0 # SysEx message start byte
0x14 # Manufacturer ID; 0x14 is actually Fairlight, but I don't forsee too many conflicts here... 
0x01 # Knobber knob channel number
0x01 # Knobber button channel number
0x0E # Knobber knob CC number
0x0F # Knobber button CC number
0x01 # Knobber button behaviour (0 = momentary, 1 = toggle)
0xF7 # SysEx message end byte

*/

#include <Bounce.h>
#include <EEPROM.h>

to this

/*
SysEx Config Message Structure:
0xF0 # SysEx message start byte
0x14 # Manufacturer ID; 0x14 is actually Fairlight, but I don't forsee too many conflicts here... 
0x01 # Knobber knob channel number
0x01 # Knobber button channel number
0x0E # Knobber knob CC number
0x0F # Knobber button CC number
0x01 # Knobber button behaviour (0 = momentary, 1 = toggle)
0xF7 # SysEx message end byte
*/

#include <Bounce.h>
#include <EEPROM.h>
#include <usbmidi.h> //see note below

Note: I noticed that you are using usbmidi without actually defining it anywhere. You probaby missed including the library.