63Views2Replies

Author Options:

attiny85 tilt switch dc motor Answered

hello my project consists of an attiny85 with an led a dc motor a tilt switch and  a button.

the button changes the pwm values and the led goes to the same values to know the strength of pwm.
 the tilt switch will activate the dc motor at the corresponding pwm speed based on how many times the button is pressed.

here is my code

const int inPin = 3;         // tilt switch or button to trigger dc motor
const int outPin = 1;       // dc motor
const int buttonPin = 2; //button to change pwm
const int ledPin = 0; // led to know the state of pwm

int pwmvalue = 255;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState =0;

int LEDstate = HIGH;      // the current state of the output pin
int reading;           // the current reading from the input pin
int previous = LOW;    // the previous reading from the input pin

// the follow variables are long's because the time, measured in miliseconds,
// will quickly become a bigger number than can be stored in an int.
long time = 0;         // the last time the output pin was toggled
long debounce = 50;   // the debounce time, increase if the output flickers

void setup()
{

  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(inPin, INPUT);
  digitalWrite(inPin, HIGH);   // turn on the built in pull-up resistor
  pinMode(outPin, OUTPUT);
}

void loop()
{

buttonState = digitalRead(buttonPin);
if (buttonPushCounter == 1)
{
  (pwmvalue == 255);}
  if (buttonPushCounter == 2)
{
  (pwmvalue == 180);}
  if (buttonPushCounter == 3)
{
  (pwmvalue == 110);}
  if (buttonPushCounter == 4)
{
  (pwmvalue == 70);}
  // 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++;
     
    } else {
      // if the current state is LOW then the button
      // wend from on to off:
     
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // 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 % 0 == 0) {
    analogWrite(ledPin,pwmvalue);
  }
  if (buttonPushCounter % 1 == 0) {
      analogWrite(ledPin,pwmvalue);

  }
  if (buttonPushCounter % 2 == 0) {
      analogWrite(ledPin,pwmvalue);
  }
  if (buttonPushCounter % 3 == 0) {
      analogWrite(ledPin,pwmvalue);
  }
 
 
  int switchstate;

  reading = digitalRead(inPin);

  // If the switch changed, due to bounce or pressing...
  if (reading != previous) {
    // reset the debouncing timer
    time = millis();
  }

  if ((millis() - time) > debounce) {
     // whatever the switch is at, its been there for a long time
     // so lets settle on it!
     switchstate = reading;

     // Now invert the output on the pin13 LED
    if (switchstate == HIGH)
      LEDstate = LOW;
    else
      LEDstate = HIGH;
  }
LEDstate = HIGH == pwmvalue;
 
  digitalWrite(outPin, pwmvalue);

  // Save the last reading so we keep a running tally
  previous = reading;

  }


is my code written correctly for my purpose is there anything i could have written better? its compiling without any problems andni havn't been able to upload because i ust back into arduino and im waiting for my attinys to come in. thank you guys for reading.

Discussions

0
None
iceng

3 years ago

You know the best written code will hide a gremlin from God and only reveal it's bizarre mischievous contradictions during first running and while there is no obvious fault I still have a thousand lifetimes to go before I ascend into an ethereal plane that would help you pre-hello.

0
None
wesm4iceng

Answer 3 years ago

Its commonly known that obvious observations made from a fellow within an interconnected network can only be taken seriously if said gremlin was really to consider cloaking itself. You however have taken the gremlins word and for that may life believe in hope.