Go Down

Topic: help with servo code (Read 808 times) previous topic - next topic

aaronyz123

I need this code to work for tomorrow so instead of teaching me could someone please just finish it, I`m kind of desperate.

I want it to start when a button is pushed and stop after about 4 minutes. Thanks

Code: [Select]

#include <Servo.h>

Servo servo1;
const int buttonPin = 2;


int pos = 0;
int buttonState = 0; 
void setup() {
  servo1.attach(9);
 
}

void loop() {
 
  pinMode(buttonPin, INPUT);
  for (pos = 0; pos <= 180; pos += 1) {

    servo1.write(pos);
   
   
    delay(4);
  }
  for (pos = 180; pos >= 0; pos -= 1) {
    servo1.write(pos);
   
    delay(4);
  }
}

J-M-L

#1
Nov 04, 2016, 06:54 am Last Edit: Nov 04, 2016, 07:18 am by J-M-L
Explain the urgency please - wasn't that the topic of your post two weeks ago

What have you done since?
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

aaronyz123

If you actually read that topic you will see that the code never worked in the end. I find it very hard to get help on this forum because everyone starts harassing me and being rude to me every time I post anything. I need the code tomorrow, you don't need to know why, if someone could actually help me that would be great.

J-M-L

You are not being harrassed

This is not a place where people code for others - or there is the gigs and collaboration forum where you can pay a programmer to use his skills and deliver code

This is not a place for students who don't do their homework and hope to get through their assignments without doing anything either

It is usually not very welcome when you show no willingness to read docs, understand example, do your own research and propose your attempt to code something matching your ask.


This is a place to expose ones work to get advices , share experience and demonstrate willingness to learn.

I can appreciate this is none of my business to know why it's urgent - but that will not attract goodwill.

You said you are a visual learner but show no result of the pointers you got in other posts and no progress whatsoever in 2 weeks

Why don't you put at the very least the structure of the code with reading the button, declaring the servo etc and an attempt at time management ?

At least give it a try in plain English - if you had to describe what the loop needs to do in very simple basic steps - what would that looks like?

I suggest you go check the code for This example for time management
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous

J-M-L

here is a bit of generic code with timing management

I split that into a few functions but should be self explanatory

Code: [Select]
enum state_t {STATE_OFF, STATE_ON};

state_t currentState;

const byte buttonPin = 7;
const byte ledPin = 13;
uint8_t ledPinState;
unsigned long startTime;
const unsigned long duration = 600000ul; // 10 minutes in ms


// ------------------------------------------
// this is where you turn your things on
// ------------------------------------------
void turnThingsOn()
{
  ledPinState = HIGH;
  digitalWrite(ledPin, HIGH);
}

// ------------------------------------------
// this is where you animate things.
// using a local time tracker here for blinking
// ------------------------------------------
void animateThings()
{
  static unsigned long lastSwitchTime = millis();
  const static unsigned long animationPace = 250ul; // every 250ms

  if (millis() - lastSwitchTime >= animationPace) {
    if (ledPinState == HIGH) ledPinState = LOW; else ledPinState = HIGH;    // ledPinState = ! ledPinState; would be the short way to do this
    digitalWrite(ledPin, ledPinState);
    lastSwitchTime += animationPace;
  }
}

// ------------------------------------------
// this is where you turn your things off
// ------------------------------------------
void turnThingsOff()
{
  ledPinState = LOW;
  digitalWrite(ledPin, LOW);
}


// ------------------------------------------
// returns true if things needs to be turned on
// here we just read if a PULL_UP button is pressed
// ------------------------------------------
bool triggerEvent()
{
  return (digitalRead(buttonPin) == LOW);
}


// ------------------------------------------
// very basic state machine. checks for the
// triggering event to go turn things ON
// and the timeout for turning things OFF
// ------------------------------------------
void handleState()
{
  // if the light is OFF and we press the button, then turn on the light
  if ((currentState == STATE_OFF) && triggerEvent()) {
    currentState = STATE_ON;
    startTime = millis();
    turnThingsOn();
  }

  // if the light is ON and time out expires, then turn off the light
  if (currentState == STATE_ON) {
    if (millis() - startTime > duration) {
      currentState = STATE_OFF;
      turnThingsOff();
    } else {
      animateThings();
    }
  }
}

// --------------------------------------------------------------------------
// --------------------------------------------------------------------------
void setup() {
  Serial.begin(115200); // only needed if you use Serial - careful might be slow and mess your timings
  pinMode(buttonPin, INPUT_PULLUP);
  pinMode(ledPin, OUTPUT);
  digitalWrite(ledPin, LOW);
  currentState = STATE_OFF;
  ledPinState = LOW;
}

void loop() {
  handleState(); // state management, call that often
  // you can do other things here as long as they are not "too long"

}


please integrate your own code in there and post back your ideas if you want more help.
Hello - Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums.
Bonjour Pas de messages privés SVP, postez dans le forum directement pour que ça profite à tous


Go Up