In for some quick help

Hi everyone,

My name is Tom and i'm new here.
For my school project i need to build a two feeders(for feeding small rodents) for the operant conditioning Operant conditioning - Wikipedia of mice using an arduino, a button and a servo.
One feeder has to give food after one click and the other feeder has to give food after a random amount of clicks (1-3).

I only have a couple of weeks left for this project and that is far too little time to understand the arduino programming code...
If someone could help me on this or program it for me, that would be great!

Kind regards,
Tom

I only have a couple of weeks left for this project and that is far too little time to understand the arduino programming code...

No it's not... have a look at these (over and above the very simple stuff on the examples page)

Counting button pushes
Random numbers
Servos

If someone could .... program it for me, that would be great!

Yes, it would, but I doubt it will happen, for a variety of reasons.

But loads of folk will help when you show what you tried and got stuck on.

okay thanks, i'll give it a try

so this is what i got after some puzzling with the different pages:

Servo myservo;

//constants
const int buttonPin = 2; //button to pin 1
const int servoPin = 1 //servo to pin 2
//initial
int buttonState = 0;
int pos = 0;
//setup
void setup() {
myservo.attach(2) ;
pinMode(buttonPin, INPUT) ;
pinMode(servoPin, OUTPUT)}

void loop(){
buttonstate = digitalRead(buttonPin)
if (buttonState == HIGH) 
{
  for(pos = 0; pos < 180; pos += 1)
  {
  myservo.write(pos) ;
  delay(15) ;
}
else {for(pos = 180; pos >= 1; pos -= 1)
{
  myservo.write(pos);
  delay(15);
}
}

i can’t test it because the arduino and servo’s are at school and i’m sick at home.
if you could please give me some feedback on it

Kind regards,
Tom

You don’t need an Arduino to compile the code remember: you can hit the Verify button instead of the Upload one.

Right now it won’t compile since you don’t have the servo library sorted, so add this line right at the top:

#include <Servo.h>

You also left ; off a bunch of lines like this one:

const int servoPin = 1

And you will also get a compile error based on this:

int buttonState = 0;

… not being the same as:

buttonstate = digitalRead(buttonPin);

Feedback depends on what you expect it to do, in this early stage.That’s going to read the button, and then do one “half-sweep” or the other based on the button.

Is that what you want for first time round?

Good start there though. Get that compiled and we can talk more.

Jim

Compiler's also going to moan about { and } not matching in number, so here's a hint:

Hover mouse over a { or a } and IDE will put a little box round its partner; helps you keep track. When you have the right number of each, hit Control-T and IDE will auto-format your sketch with snazzy indents.

fixed it now

#include <Servo.h>
Servo myservo;

//constants
const int buttonPin = 2; //button to pin 1
const int servoPin = 1; //servo to pin 2
//initial
int buttonState = 0;
int pos = 0;
//setup
void setup() {
myservo.attach(2) ;
pinMode(buttonPin, INPUT) ;
pinMode(servoPin, OUTPUT);}

void loop(){
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) 
{
  for(pos = 0; pos < 180; pos += 1)
  {
  myservo.write(pos) ;
  delay(15) ;
}
}else {for(pos = 180; pos >= 1; pos -= 1)
{
  myservo.write(pos);
  delay(15);}
}
}

now i would like the servo to only move back and forth when the button is pressed, should i put the command line that is stated under else after the delay? like this: delay(15); for(pos = 180; pos >= 1; pos -= 1)

I don't understand your question. What does the present code do and how would you like it to work?

...R

In the first post i wrote what i wanted it to do, and the code tells you what it does now and if i understand Jim correctly, it sweeps the servo 180 degrees depending on the button state.

But i'll make my question more clearly: looking at the code in my earlier post, what do i have to do to make the servo go back and forth when the button is pressed?

Well, look at it like this. I'm willing to help if it is not too much trouble. And I know if it was my project that I could get it to work.

The general purpose of this forum is to teach newcomers, not to do the work for them. Part of the purpose of my question was to get you to think about the problem in a way that might lead you to a solution without my help.

So let me repeat my question (to save you having to read earlier posts).

What does the present code do and how would you like it to work?

...R

Okay, i’m not trying to be disrespectful, but i’ll rephrase my question once more: the servo now sweeps 180 degrees depending op the button state ( HIGH or LOW).

What i want it to do is: when the button is pushed(HIGH) i want the servo to sweep 180 degrees and back to its initial position.
so should i put the line of code after(see earlier post) else() under the if() and leave the else() blank?

Does this make it clear what i mean?
sorry but i cant post the whole program, because this is written from my mobile phone.

That's clearer.

I think it will do what you want if you remove the else and its corresponding {} from your existing code.

...R

but according to the progranmed code, it will sweep 180 degrees but not sweep back? should i add this piece of code after the delay? for(pos = 180; pos >= 1; pos -= 1)
so it will sweep back?

the servo keeps sweeping back and forth, can anyone help me on this?

#include <Servo.h>
Servo myservo;

//constants
const int buttonPin = 2; //button to pin 1
const int servoPin = 3; //servo to pin 2
//initial
int buttonState = 0;
int pos = 0;
//setup
void setup() {
myservo.attach(3) ; //servo to pin
pinMode(buttonPin, INPUT) ; //set buttonpin as input
pinMode(servoPin, OUTPUT);} //set servopin as output

void loop(){
buttonState = digitalRead(buttonPin); 
if (buttonState == HIGH) //high = buttton pressed
{
  for(pos = 0; pos < 180; pos += 1) //button pressed
  {
  myservo.write(pos) ;
  delay(15) ;
}
}

}

the servo wires are connected to ground, 3.3v and port 3
the button wires are connected to ground 5v and port 2

the servo keeps sweeping back and forth, while the button is not pressed, could someone please help me, because i don’t know what is wrong in the coding?

if (buttonState == LOW) //high = buttton pressed

From the comment, your code is commanding the servo when the button is not pressed.

try this?

if (buttonState == HIGH) //high = buttton pressed

i already tried that, i only changed high to low, that why it said low. forgot to change it back to high :sweat_smile:

the servo wires are connected to ground, 3.3v and port 3
the button wires are connected to ground 5v and port 2

Generally, you need a separate power supply for a servo - they draw too much current an may damage your arduino.

Sounds like you have a floating input. Take a look at the documentation for the pinmode command and try using the input_pullup option instead. You'll need to wire the button between your sensing pin and ground and check for low to indicate a button press.

I’m just a beginner in this so i’d like to keep the coding simple.
but i’ve got this now, testing it this afternoon.

#include <Servo.h>
Servo myservo;

//constants
const int buttonPin = 2 ; //button to pin 1
const int servoPin = 9 ; //servo to pin 2
 
//initial
int buttonPushCounter = 0 ;
int buttonState = 0 ;
int lastButtonState = 0 ;
int pos = 0 ;
//setup
void setup() 
{
myservo.attach(9) ; //servo to pin
pinMode(buttonPin, INPUT) ; //set buttonpin as input
pinMode(servoPin, OUTPUT) ; //set servopin as output
Serial.begin(9600);}

void loop(){
buttonState = digitalRead(buttonPin) ; 
// compare the buttonState to its previous state
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  
  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 1 == 0) {
    for(pos = 0; pos < 180; pos += 1) //button pressed
  {
  myservo.write(pos) ;
  delay(15) ;
  }
  }
}

is this going to work?

The usual way to answer the question “is this going to work?” is to try it.

You have a long comment that doesn’t seem to make any sense in the context of the code that follows it

  // turns on the LED every four button pushes by 
  // checking the modulo of the button push counter.
  // the modulo function gives you the remainder of 
  // the division of two numbers:
  if (buttonPushCounter % 1 == 0) {
    for(pos = 0; pos < 180; pos += 1) //button pressed
  {
  myservo.write(pos) ;
  delay(15) ;
  }
  }

What is the purpose (if any) of counting the button presses?

…R

Thanks, but i won't be needing help on this anymore, my school decided to forbid any animals kept inside the school, so i'm having to change my biology project. But everyone thanks for your help!