attiny 25 programmeren

Hallo iedereen

Project:

Ik wil onderstaande sketch op een attiny 25 programmeren.
Het project is van deze link: GitHub - adafruit/Adafruit_Brain-Machine-Kit: The Brain Machine Kit
Nu is het zo dat de code van de Brain-Machine niet als Sketch in arduino geschreven is.
Dus ik heb aan de code: “void setup() {“ en “void loop() {
// no need to repeat }” toegevoegd.

Probleem:

Ik krijg deze foutmelding:
Exit status 1
A function-definition is not allowed here before ‘{’ token

Verdere info:

Instellingen van de Arduino UNO:
Board: Attiny 25/45/85
Processor: attiny 25
Clock internal 8 Mhz
Programmer: Arduino ISP
Bootloader is gebrand
Wat is hier het probleem?
Alvast bedankt voor de hulp.
Gr ben
```
**#include <avr/io.h>            // this contains all the IO port definitions
#include <avr/interrupt.h>      // definitions for interrupts
#include <avr/sleep.h>          // definitions for power-down modes
#include <avr/pgmspace.h>      // definitions or keeping constants in program memory
void setup() {

struct brainwaveElement {
  char bwType;  // ‘a’ for Alpha, ‘b’ for Beta, ‘t’ for Theta, or ‘d’ for Delta (‘0’ signifies last entry in table
  unsigned long int bwDuration;  // Duration of this Brainwave Type (divide by 10,000 to get seconds)
} const brainwaveTab PROGMEM = {
  { ‘b’, 600000 },
  { ‘a’, 100000 },
  { ‘b’, 200000 },
  { ‘a’, 150000 },
  { ‘b’, 150000 },
  { ‘a’, 200000 },
  { ‘b’, 100000 },
  { ‘a’, 300000 },
  { ‘b’,  50000 },
  { ‘a’, 600000 },
  { ‘t’, 100000 },
  { ‘a’, 300000 },
  { ‘t’, 200000 },
  { ‘a’, 300000 },
  { ‘t’, 300000 },
  { ‘a’, 150000 },
  { ‘t’, 600000 },
  { ‘a’, 150000 },
  { ‘b’,  10000 },
  { ‘a’, 150000 },
  { ‘t’, 600000 },
  { ‘d’,  10000 },
  { ‘t’, 100000 },
  { ‘d’,  10000 },
  { ‘t’, 100000 },
  { ‘d’,  10000 },
  { ‘t’, 300000 },
  { ‘a’, 150000 },
  { ‘b’,  10000 },
  { ‘a’, 150000 },
  { ‘t’, 300000 },
  { ‘a’, 150000 },
  { ‘b’,  10000 },
  { ‘a’, 200000 },
  { ‘b’,  50000 },
  { ‘a’, 200000 },
  { ‘b’, 150000 },
  { ‘a’, 150000 },
  { ‘b’, 200000 },
  { ‘a’, 100000 },
  { ‘b’, 250000 },
  { ‘a’,  50000 },
  { ‘b’, 600000 },
  { ‘0’,      0 }
};

// This function delays the specified number of 1/10 milliseconds
void delay_one_tenth_ms(unsigned long int ms) {
  unsigned long int timer;
  const unsigned long int DelayCount=87;  // this value was determined by trial and error

while (ms != 0) {
    // Toggling PB5 is done here to force the compiler to do this loop, rather than optimize it away
    for (timer=0; timer <= DelayCount; timer++) {PINB |= 0b0100000;};
    ms–;
  }
}

void blink_LEDs( unsigned long int duration, unsigned long int onTime, unsigned long int offTime) {
  for (int i=0; i<(duration/(onTime+offTime)); i++) {
    PORTB |= 0b00001100;          // turn on LEDs at PB3, PB2
    delay_one_tenth_ms(onTime);  //  for onTime
    PORTB &= 0b11110011;          // turn off LEDs at PB3, PB2
    delay_one_tenth_ms(offTime);  //  for offTime
  }
}

void do_brainwave_element(int index) {
    char brainChr = pgm_read_byte(&brainwaveTab[index].bwType);
    if (brainChr == ‘b’) {
      // Beta
      // start Timer1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //  to Right ear speaker through output OC1A (PB1, pin 6)
      OCR1C = 71;  // T1 generates 217.014Hz, for a binaural beat of 16.7Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //  onTime = 30.0ms, offTime = 29.9ms → 16.7Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 300, 299 );
      return;  // Beta
    }

else if (brainChr == ‘a’) {
      // Alpha
      // start Timer1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //  to Right ear speaker through output OC1A (PB1, pin 6)
      OCR1C = 73;  // T1 generates 211.149Hz, for a binaural beat of 10.8Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //  onTime = 46.2ms, offTime = 46.2ms → 10.8Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 462, 462 );
      return;  // Alpha
    }

else if (brainChr == ‘t’) {
        // PORTB &= 0b00001100;  // (for debugging purposes only – commented out for SLM)
        // PORTB |= 0b00100000;
      // Theta
      // start Timer1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //  to Right ear speaker through output OC1A (PB1, pin 6)
      OCR1C = 75;  // T1 generates 205.592Hz, for a binaural beat of 5.3Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //  onTime = 94.8ms, offTime = 94.9ms → 5.3Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 948, 949 );
      return;  // Theta
    }

else if (brainChr == ‘d’) {
        // PORTB &= 0b00001100;  // (for debugging purposes only – commented out for SLM)
        // PORTB |= 0b00010000;
      // Delta
      // start Timer1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //  to Right ear speaker through output OC1A (PB1, pin 6)
      OCR1C = 76;  // T1 generates 202.521Hz, for a binaural beat of 2.6Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      //  onTime = 1922ms, offTime = 1923ms → 2.6Hz
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 1922, 1923 );
      return;  // Delta
    }

// this should never be executed, since we catch the end of table in the main loop
    else {
        // PORTB &= 0b00001100;  // (for debugging purposes only – commented out for SLM)
        // PORTB |= 0b00000010;
      return;      // end of table
    }
}

static volatile unsigned int power attribute ((section (".noinit")));

int main(void) {

if (power == 0) {
    power = 1;
    // Shut down everything and put the CPU to sleep
    TCCR0B &= 0b11111000;  // CS02:CS00=000  to stop Timer0 (turn off audio in Right ear speaker)
    TCCR1  &= 0b11110000;  // CS13:CS10=0000 to stop Timer1 (turn off audio in Left ear speaker)
    MCUCR |= 0b00100000;  // SE=1 (bit 5)
    MCUCR |= 0b00010000;  // SM1:0=10 to enable Power Down Sleep Mode (bits 4, 3)
    delay_one_tenth_ms(10000);  // wait 1 second
    PORTB = 0x00;          // turn off all PORTB outputs
    DDRB = 0x00;          // make PORTB all inputs
    sleep_cpu();          // put CPU into Power Down Sleep Mode
  }

power = 0;

TIMSK = 0x00;       
  DDRB = 0b00001111; 
  PORTB = 0x00;

TCCR0A = 0b01000010; 
                     
                       
                       
  TCCR0B = 0b00000100; 
                       
                       
  OCR0A = 77;

TCCR1 = 0b10011001; 
  int j = 0;
  while (pgm_read_byte(&brainwaveTab[j].bwType) != ‘0’) {  // ‘0’ signifies end of table
    do_brainwave_element(j);
    j++;
  }

TCCR0B &= 0b11111000; 
  TCCR1  &= 0b11110000; 
  MCUCR |= 0b00100000; 
  MCUCR |= 0b00010000; 
  delay_one_tenth_ms(10000); 
  PORTB = 0x00;         
  DDRB = 0x00;         
  sleep_cpu();         
}
}

void loop() {
  // no need to repeat
}
__
```**__

Hoi ben009, welkom.

Je hebt vermoedelijk een fout gemaakt met de { } haakjes.
Je kunt in de IDE je cursor achter zo'n { haakje zetten, en de IDE zet dan een dun blauw lijntje om de bijbehorende }.
Je kunt ook de cursor bij een } zetten voor de bijbehorende {

Hoi Ben,

Het is geen probleem met de { } haakjes !!!

Aan de code te zien is deze niet gemaakt met Arduino Sketches maar met c++
Zoals je al aangaf in je post. Door gewoon effen
"void setup() {" en "void loop() { // no need to repeat }" in de code te zetten, gaat dat niet werken.

De code zou moeten aangepast worden zodat deze werkt in Arduino Sketches.

Dus de oplossing is: een nieuwe post plaatsen met als Topic: script c++ omzetten naar Arduino Sketches

Groeten

avrprof

Hallo,

Ik heb nu de code aangepast voor het Arduino platform.

Nu wel veel fout meldingen…

#include <avr/io.h>             // this contains all the IO port definitions
#include <avr/interrupt.h>      // definitions for interrupts
#include <Tone.h>               // Include the arduino tone library
#include <avr/sleep.h>          // definitions for power-down modes
#include <avr/pgmspace.h>       // definitions or keeping constants in program memory
#include <SoftwareSerial.h>


#define rightEyeRed 7 // Define pinout for right eye
#define leftEyeRed 2 // Define pinout for left eye

#define wakePin 1 // the input pin where the pushbutton is connected.

struct brainwaveElement {
  char bwType;  // 'a' for Alpha, 'b' for Beta, 't' for Theta, or 'd' for Delta ('0' signifies last entry in table
  unsigned long int bwDuration;  // Duration of this Brainwave Type (divide by 10,000 to get seconds)
} const brainwaveTab[] PROGMEM = {
  { 'b', 600000 },
  { 'a', 100000 },
  { 'b', 200000 },
  { 'a', 150000 },
  { 'b', 150000 },
  { 'a', 200000 },
  { 'b', 100000 },
  { 'a', 300000 },
  { 'b',  50000 },
  { 'a', 600000 },
  { 't', 100000 },
  { 'a', 300000 },
  { 't', 200000 },
  { 'a', 300000 },
  { 't', 300000 },
  { 'a', 150000 },
  { 't', 600000 },
  { 'a', 150000 },
  { 'b',  10000 },
  { 'a', 150000 },
  { 't', 600000 },
  { 'd',  10000 },
  { 't', 100000 },
  { 'd',  10000 },
  { 't', 100000 },
  { 'd',  10000 },
  { 't', 300000 },
  { 'a', 150000 },
  { 'b',  10000 },
  { 'a', 150000 },
  { 't', 300000 },
  { 'a', 150000 },
  { 'b',  10000 },
  { 'a', 200000 },
  { 'b',  50000 },
  { 'a', 200000 },
  { 'b', 150000 },
  { 'a', 150000 },
  { 'b', 200000 },
  { 'a', 100000 },
  { 'b', 250000 },
  { 'a',  50000 },
  { 'b', 600000 },
  { '0',      0 }
};


// This function delays the specified number of 1/10 milliseconds
void delay_one_tenth_ms(unsigned long int ms) {
  unsigned long int timer;
  const unsigned long int DelayCount=87;  // this value was determined by trial and error

  while (ms != 0) {
    // Toggling PB5 is done here to force the compiler to do this loop, rather than optimize it away
    for (timer=0; timer <= DelayCount; timer++) {PINB |= 0b0100000;};
    ms--;
  }
}

void setup()  { 
  
  pinMode(rightEyeRed, OUTPUT); // Pin output at rightEyeRed
  pinMode(leftEyeRed, OUTPUT); // Pin output at leftEyeRed
  pinMode(wakePin, INPUT); // Pin input at wakePin
  Serial.begin(9600); // 
//  myTimer(); // Comment this out when you've adjusted DelayCount
}



void loop() {
  int j = 0;
  while (pgm_read_byte(&brainwaveTab[j].bwType) != '0') {  // '0' signifies end of table
    do_brainwave_element(j);
    j++;
  }
 
  // Shut down everything and put the CPU to sleep
  analogWrite(rightEyeRed, 255);  // common anode - 
  analogWrite(leftEyeRed, 255);  // HIGH means 'off'
 
  
// myTimer(); // Comment this out when you've adjusted DelayCount
  sleepNow();
}



void blink_LEDs( unsigned long int duration, unsigned long int onTime, unsigned long int offTime) {
  for (int i=0; i<(duration/(onTime+offTime)); i++) {   
    analogWrite(rightEyeRed, 0);  // common anode -
    analogWrite(leftEyeRed, 0);  // LOW means 'on'
    // turn on LEDs     
    delay_one_tenth_ms(onTime);   //   for onTime     
    
    analogWrite(rightEyeRed, 255);  // common anode - 
    analogWrite(leftEyeRed, 255);  // HIGH means 'off'
   // turn off LEDs    
    delay_one_tenth_ms(offTime);  //   for offTime   
  } 
} 

void alt_blink_LEDs( unsigned long int duration, unsigned long int onTime, unsigned long int offTime) {
  for (int i=0; i<(duration/(onTime+offTime)); i++) {   
    analogWrite(rightEyeRed, 0);  // common anode -
    analogWrite(leftEyeRed, 255);  // LOW means 'on'
    // turn on LEDs     
    delay_one_tenth_ms(onTime);   //   for onTime     
    
    analogWrite(rightEyeRed, 255);  // common anode - 
    analogWrite(leftEyeRed, 0);  // HIGH means 'off'
   // turn off LEDs    
    delay_one_tenth_ms(offTime);  //   for offTime   
  } 
} 


void do_brainwave_element(int index) {     
  char brainChr = pgm_read_byte(&brainwaveTab[index].bwType);     
    
  switch (brainChr) {
    case 'b':         
      // Beta       
     
      //  Generate binaural beat of 14.4Hz       
      //  delay for the time specified in the table while blinking the LEDs at the correct rate       
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 347, 347 );
      return;   // Beta
 
    case 'B':         
      // Beta - with alternating blinks
      
      //  Generate binaural beat of 14.4Hz       
      //  delay for the time specified in the table while blinking the LEDs at the correct rate       
      alt_blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 347, 347 );
      return;   // Beta - with alternating blinks
 
    case 'a':
      // Alpha
      
      // Generates a binaural beat of 11.1Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 451, 450 );
      return;   // Alpha
 
    case 'A':
      // Alpha
      
      // Generates a binaural beat of 11.1Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      alt_blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 451, 450 );
      return;   // Alpha - with alternating blinks
 
    case 't':
      // Theta
      // start Timer 1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //   to Right ear speaker through output OC1A (PB3, pin 15)
       
      // Generates a binaural beat of 6.0Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 835, 835 );
      return;   // Theta
 
    case 'T':
      // Theta
      // start Timer 1 with the correct Offset Frequency for a binaural beat for the Brainwave Type
      //   to Right ear speaker through output OC1A (PB3, pin 15)
       
      // Generates a binaural beat of 6.0Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      alt_blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 835, 835 );
      return;   // Theta - with alternating blinks
 
    case 'd':
      // Delta
      
      // Generates a binaural beat of 2.2Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 2253, 2253 );
      return;   // Delta
 
    case 'D':
      // Delta
      
      // Generates a binaural beat of 2.2Hz
      // delay for the time specified in the table while blinking the LEDs at the correct rate
      alt_blink_LEDs( pgm_read_dword(&brainwaveTab[index].bwDuration), 2253, 2253 );
      return;   // Delta
 
     
    // this should never be executed, since we catch the end of table in the main loop
    default:
      return;      // end of table
  }
}



void sleepNow()         // here we put the arduino to sleep
{
    
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);   // sleep mode is set here

    sleep_enable();          // enables the sleep bit in the mcucr register
                             // so sleep is possible. just a safety pin 

    

    attachInterrupt(0,wakeUpNow, LOW); // use interrupt 0 (pin 2) and run function
                                       // wakeUpNow when pin 2 gets LOW 

    sleep_mode();            // here the device is actually put to sleep!!
                             // THE PROGRAM CONTINUES FROM HERE AFTER WAKING UP

    sleep_disable();         // first thing after waking from sleep:
                             // disable sleep...
    detachInterrupt(0);      // disables interrupt 0 on pin 2 so the 
                             // wakeUpNow code will not be executed 
                             // during normal running time.

}


void wakeUpNow()        // here the interrupt is handled after wakeup
{
  // execute code here after wake-up before returning to the loop() function
  // timers and code using timers (serial.print and more...) will not work here.
  // we don't really need to execute any special functions here, since we
  // just want the thing to wake up
}

void myTimer(){
  Serial.println("  ");
  Serial.print("Time: ");
  long int time = millis();
  //prints time since program started
  Serial.print(time);
  Serial.println("  ");
  // This function is a little quirky, but it works.

dit zijn de fout meldingen:

Arduino:1.8.9 (Windows Store 1.8.21.0) (Windows 10), Board:"ATtiny25/45/85, ATtiny25, Internal 8 MHz"

C:\Users\AppData\Local\Temp\arduino_modified_sketch_722270\sketch_jun12a.ino: In function 'void setup()':

sketch_jun12a:82:3: error: 'Serial' was not declared in this scope

Serial.begin(9600); //

^

C:\Users\AppData\Local\Temp\arduino_modified_sketch_722270\sketch_jun12a.ino: In function 'void myTimer()':

sketch_jun12a:250:3: error: 'Serial' was not declared in this scope

Serial.println(" ");

^

sketch_jun12a:255:23: error: expected '}' at end of input

Serial.println(" ");

^

exit status 1
'Serial' was not declared in this scope

Dit rapport zou meer informatie bevatten met
"Uitgebreide uitvoer weergeven tijden compilatie"
optie aan in Bestand -> Voorkeuren.

ATTinies hebben geen hardware Serial ingebouwd. Dus kan je die niet gebruiken en moet je een andere oplossing bedenken als je serieel wilt gebruiken. ff googelen...