studieproject

Arduino specifieke Software
Berichten: 12
Geregistreerd: 30 Jan 2017, 17:46

Re: studieproject

Berichtdoor Carlijn » 31 Jan 2017, 16:32

Code: Alles selecteren
// STEPPER included to give reward automatically.
// (can be downloaded at: http://www.airspayce.com/mikem/arduino/AccelStepper/)
#include <AccelStepper.h>

//Define the stepper motor and the connection between Arduino Uno and the motor driver boards:
//pin 1 indicates that the stepper motor 1 is driven by a driver board, pin 2 is connected to "step"of the driver board, pin 3 is connected to "direction"of the driver board.
AccelStepper stepper(1, 2, 3);

//define the rotation angle of the feed dispensing disk
//This is the disk rotation angle in the stepper motor in microsteps: a disk contains 50 holes and the used stepper motors divide one full circle in 1600 microsteps, meaning that there is a hole aver 32 microsteps (1600/50=32):
#define rotationAngleDisk 32

#define calibrationAngleDisk 3

//This is the amount of presses needed in order to receive a food reward when pressing
#define neededPressesButton 1

//This is the time in milliseconds during which the program will pause after a reward has been received
#define pauseTimeAfterReward 2500

//This is the extra amount of presses needed for a new reward
#define extraPressesNeeded 2




// Constants won't change. They're used here to set pin numbers, to define to which Arduino Uno pins are connected:
int buttonPin = 4;                                      //Button is connected to Arduino pin 4
int calibrationPin = 5;                                 //Calibration button is connected to Arduino pin 5
const int pauseButtonCountReward = 500;                  //This is the amount of time button presses are not counted after the reward has been given, to prevent accidental presses for the next reward when consuming.

// Variables will change:
int rewardCounter = 0;                                  //Counts the amount of rewards received at pedal
int totalPress = 0;                                     //Counts the total amount of presses without a reward being received because the timeAfterReward was still running
int totalLastPress = 0;                                 //Counts the amount of presses after a reward being received because the timeAfterReward was still running
int pressesForReward = extraPressesNeeded * rewardCounter + 1; //Counts the amount of presses needed for the next reward

// The following variables are unsigned long's because the time, measured in milliseconds, will quickly become a bigger number than can be stored in an int
unsigned long totalTime = 0;                         //This is the total amount has passed after (initial value is zero)
unsigned long timeAfterReward = 0;                    //This is the amount of time that has passed after a reward is received (initial value is zero)


boolean lastButtonState = LOW;

// When using a hardware switch you may want to debounce the input so that you don't multiple trigger on a noisy edge transition.
//Function for removing the debounce effect of button 1:
boolean debounce(boolean last)
{
  boolean current = digitalRead(buttonPin);
  if (last != current)
  {
    delay(5);
    current = digitalRead(buttonPin);
  }
  return current;
}

void setup() {
  // put your setup code here, to run once. It is used to initialize variables, pin modes, start using libraries, etc.

  pinMode(buttonPin, INPUT);                            //Sets button as an input signal to the Arduino
  pinMode(calibrationPin, INPUT);                       //Sets calibration button as an input signal to the Arduino

  Serial.begin(9600);                                   //Initializes serial communication, 9600bits per second

}   //close setup

//The loop functions loops consecutively, allowing the program to change and respond, it is used to actively control the Ardino board:
void loop() {       // put your main code here, to run repeatedly:

  //The code below results in output text in the serial monitor:
  Serial.print("Amount of presses needed for food reward: ");
  Serial.print(neededPressesButton);

  //Variable indicating the current state of button (the debounce function is used here)
  boolean currentButtonState = debounce(lastButtonState);

  //If the defined amount of pause time after a reward has not passed yet AND timeAfterReward is not equal to zero (because timeAfterReward is zero at the start of the program)
  //It pauses the program after every reward (during the amount of milliseconds set at the top of the program code:pauseTimeAfterReward
  if (millis() - timeAfterReward <= pauseTimeAfterReward)
  {
    //nothing should happen here, it pauses the program after everey reward (during the amount of milliseconds set at the top of the program code:pauseTimeAfterReward

  }
  else
  {
    if (currentButtonState != lastButtonState)  // if the current state of the button is not equel to the last ste of the butoon (pressed or released)
    {
      if  (currentButtonState == HIGH)        // pedal is pressed when current button state is high
      {

        totalPress++;                         // totalPress will increase with 1 unit;
        totalLastPress++;                     // TotalLastPress wil increase with 1 unit: amount of presses after a reward.


      }
    }

    if (totalLastPress == pressesForReward) // if totalLastPress (totalamount of presses after last reward) is  equal to amount of presses needed for reward, a reward is given
    {
      stepper.setMaxSpeed(3000);               //maximum speed of stepper (in microsteps/s)
      stepper.setAcceleration(3000);           //acceleration of stepper (in microsteps/s)
      stepper.moveTo(stepper.currentPosition() + calibrationAngleDisk);   //stepper will move forward to amount of steps defined in calibrationAngleDisk
      tone(12, 16000, 100);                  // the speaker( connected to Arduino pin 12) produces a 1600Hz sound during 100,s (0.1s), cats know a reward wil come.
      totalLastPress = 0;                    // TotalLastPress is set to zero after a reward is given; meaning that counting starts over again.
      rewardCounter++;                       // rewardCounter will increase with 1 unit

      //the following information is printed
      Serial.println("total time: ");
      Serial.print(totalTime);
      Serial.print(" ms, ");

      Serial.print("\t");                    // printing a tab
      Serial.print("Pedal is pressed at milliseconds after last reward: ");
      Serial.print(timeAfterReward);
      Serial.print(" ms, ");

      Serial.print("\t");                    // printing a tab
      Serial.print("resulting in food reward number: ");
      Serial.print(rewardCounter);

      Serial.print("\t");                    // printing a tab
      Serial.print("Total number of presses is: ");
      Serial.print(totalPress);



    }

    else                                    // if the amount of presses at the pedal is lower that the amount of presses needed for receiving a reward.
    {
      if (totalLastPress != pressesForReward)// if totalLastPress (totalamount of presses after last reward is not equal to amount of presses needed for reward)
      {

      }
    }
  }

  lastButtonState = currentButtonState;     // at the end of every loop the last state of the button is set equal to the current state of the button
  stepper.run();                            //causes stepper motor to run, this command has to be called as frequently as possible and is therefor found in the main loop

}




Advertisement

Berichten: 4064
Geregistreerd: 16 Okt 2013, 14:31
Woonplaats: s hertogenbosch

Re: studieproject

Berichtdoor shooter » 31 Jan 2017, 20:55

omdat je commentaren nogal lang zijn zet deze beter in /* */ want dan heb je daar geen last van.
de laatsste if lijkt mij niet nodig, want die zit al in de else.

hardware:
ik denk dat het pedaal niet goed zit, gebruik daar input_PULLUP voor dan kun je de pedaal gewoon naar de gnd schakelen. en heb je geen weerstand nodig.
gebruik led (13) om de tijd te zien, als er op het pedaal gedrukt kan worden.
zorg dat de kat weet dat die het pedaal in heeft geduwd.
paul deelen
shooter@home.nl

Berichten: 12
Geregistreerd: 30 Jan 2017, 17:46

Re: studieproject

Berichtdoor Carlijn » 06 Feb 2017, 16:01

Code: Alles selecteren
//This is the extra amount of presses needed for a new reward
#define extraPressesNeeded 2

// this constant won't change:
const int  buttonPin = 4;    // the pin that the pushbutton is attached to
const int ledPin = 13;       // the pin that the LED is attached to


// Variables will change:
int buttonPushCounter = 0;   // counter for the total number of button presses
int rewardCounter = 0;      // counts the amount of rewards
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button
int totLastPress = 0;         // counts te amount of presses after a reward is received
int pressesForReward = (extraPressesNeeded * rewardCounter) + 1; // counts the amount of presses needed for the next reward

unsigned long totalTime = 0;

void setup() {
  // initialize the button pin as a input:
  pinMode(buttonPin, INPUT);
  // initialize the LED as an output:
  pinMode(ledPin, OUTPUT);
  // initialize serial communication:
  Serial.begin(9600);
}


void loop() {
  // read the pushbutton input pin:
  buttonState = digitalRead(buttonPin);
  pressesForReward == (extraPressesNeeded * rewardCounter) + 1;

 
  if (buttonState != lastButtonState) {
 
    if (buttonState == HIGH) {
   
      buttonPushCounter++;
      totLastPress++;



      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
      Serial.print ("totallastpress ");
      Serial.println (totLastPress);

      // Delay a little bit to avoid bouncing
      delay(50);


    }
  }
  if (totLastPress == pressesForReward)
  {
    tone(12, 2000, 100);
    rewardCounter++;       //rewardCounter will increase with 1 unit
    totLastPress = 0;      //totalLastPress is set to 0
    Serial.print ("rewards");
    Serial.println (rewardCounter);
  }
  else


  {
    tone(12, 5000, 100);




  }


  lastButtonState = buttonState;
}



Na veel proberen krijg ik eindelijk wat voor elkaar in versimpelde vorm.
Alleen bij het invoegen van de volgende stap (rewards) krijg ik problemen.
Het systeem geeft aan dat bij elke druk knop beweging er een reward komt. Helaas moet dit niet (pressesForReward == (extraPressesNeeded * rewardCounter) + 1;). Wil iemand met mij mee kijken naar de fout. Ik loop erop vast

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: studieproject

Berichtdoor Koepel » 06 Feb 2017, 17:00

Dit:
Code: Alles selecteren
int pressesForReward = (extraPressesNeeded * rewardCounter) + 1; // counts the amount of presses needed for the next reward
staat niet bij de code, maar bij het initialiseren van de variabele.

In de taal 'c' mag je bij het initialiseren van een variabele een berekening doen. Dat is vaak niet nodig, maar het mag wel. Nu wordt eenmalig de berekening "(2 * 0) + 1" gedaan en die waarde wordt eenmalig in "pressesForReward" gestopt.

Wil je echt code gaan uitvoeren en berekeningen gaan doen, dan hoort dat in setup() en in loop().

Berichten: 12
Geregistreerd: 30 Jan 2017, 17:46

Re: studieproject

Berichtdoor Carlijn » 06 Feb 2017, 17:07

Koepel schreef:Dit:
Code: Alles selecteren
int pressesForReward = (extraPressesNeeded * rewardCounter) + 1; // counts the amount of presses needed for the next reward
staat niet bij de code, maar bij het initialiseren van de variabele.

In de taal 'c' mag je bij het initialiseren van een variabele een berekening doen. Dat is vaak niet nodig, maar het mag wel. Nu wordt eenmalig de berekening "(2 * 0) + 1" gedaan en die waarde wordt eenmalig in "pressesForReward" gestopt.

Wil je echt code gaan uitvoeren en berekeningen gaan doen, dan hoort dat in setup() en in loop().


Bedankt!!
Dit heeft gelopen.

Nu alleen uitvogelen waarom speaker non stop geluid maakt als de arduino aan de computer zit. Waarschijnlijk verkeerd aangesloten ofzo.

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: studieproject

Berichtdoor Koepel » 06 Feb 2017, 17:25

Bij een if-statement is het waar of niet waar. Wanneer voor beide condities de tone() aanzet, dat zet je dus altijd de tone() aan.
Code: Alles selecteren
  if (totLastPress == pressesForReward)
  {
    tone(12, 2000, 100);
  }
  else
  {
    tone(12, 5000, 100);
  }

Berichten: 12
Geregistreerd: 30 Jan 2017, 17:46

Re: studieproject

Berichtdoor Carlijn » 06 Feb 2017, 17:28

Koepel schreef:Bij een if-statement is het waar of niet waar. Wanneer voor beide condities de tone() aanzet, dat zet je dus altijd de tone() aan.
Code: Alles selecteren
  if (totLastPress == pressesForReward)
  {
    tone(12, 2000, 100);
  }
  else
  {
    tone(12, 5000, 100);
  }



maar dat zou dan toch voor 100 msec zijn?
Ik wil bij elke druk een tone, maar twee verschillenden.

Op dit moment klinkt zelfs zonder de seriele monitor opgestart te hebben non stop een tone. En dat klopt geloof ik niet.

Gebruikers-avatar
Berichten: 2655
Geregistreerd: 06 Aug 2016, 01:03

Re: studieproject

Berichtdoor Koepel » 06 Feb 2017, 18:59

De loop() draait heel snel, dus zodra de tone() is afgelopen, dan wordt binnen een paar microseconden er weer een nieuwe tone() aangeroepen. Die onderbreking hoor je echt niet. Met dat if-statement maak je altijd een tone(), ongeacht of er iets wordt ingedrukt.

Ga eens met een bezem door de code ;) Zet alle haakjes en inspringen en nieuwe regels goed, en voeg wat commentaar toe. Als je sketch goed leesbaar is, dan is ook de structuur van de code beter te zien.

Berichten: 12
Geregistreerd: 30 Jan 2017, 17:46

Re: studieproject

Berichtdoor Carlijn » 07 Feb 2017, 14:01

Nogmaals bedankt allemaal!!

Ik heb veel aan de hulp gehad en kan vertellen dat alles werkt!

Vorige

Terug naar Arduino software

Wie is er online?

Gebruikers in dit forum: Geen geregistreerde gebruikers en 16 gasten