65Views4Replies

Author Options:

attiny85 to control two different jobs through a switch? Answered

What i really want is to piece up 2 sketches together in attiny85 programming,the result is to enable it to be controlled through a switch.the 2 sketched are pretty long.I don't know how bcz i am a newbie in programming.at first i thought of 'Case,break'syntax,but later found it may not suitable.

The pins i choose is Pin4(Analog2) physical pin3 of attiny85 to control of WS2812B light strip.another Pin1(PWM)physical Pin6 to control a speaker.

#include <Adafruit_NeoPixel.h>
#include <avr/power.h>

#define PIN 4

Adafruit_NeoPixel strip = Adafruit_NeoPixel(30, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
 
  Serial.begin(9600);
  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}


void loop() {
 
  int sensorValue = analogRead(A0);

  Serial.println(sensorValue);//Just for the calipration
 


  if(sensorValue > 10)
  {
rainbowCycle(0);
}
if(sensorValue >30){

theaterChase(strip.Color(255,0,0),10);
theaterChase(strip.Color(255,0,0),10);
theaterChase(strip.Color(0,0,255),10);
}
if(sensorValue >40){
  colorWipe(strip.Color(255,0,0),10);
  colorWipe(strip.Color(0,255,0),10);
  colorWipe(strip.Color(0,0,255),10);
}
}

============
/* ATtiny85 Speaker */


// constants
const int Speaker = 1;             // speaker pin

void setup()
{
  pinMode(Speaker, OUTPUT);


void playBeep(void)
{
    for (int i=0; i < 500; i++)
    {
      digitalWrite(Speaker, HIGH);
      delay(1);
      digitalWrite(Speaker, LOW);
      delay(1);
    }
}

Tags:coding

Discussions

0
None
mpilchfamily

4 years ago

Is that how you have the final sketch laid out?

Those are actually really short sketches. But i don't see where you've put the button and checked for the button to be pressed. What i see here is lights changing based on what i can only guess is a potentiometer attached to the analog pin and you have a digital pin going high and low on a speaker. Is that even producing any sound for you?

0
None
gada888mpilchfamily

Answer 4 years ago

I found the balance to it.just use two chips.suppose the chip is cheap enough.

0
None
rickharris

4 years ago

1. If you want someone else to look at your prog - Annotate EVERY line even if it looks obvious to you.

2. (thanks for at least some annotation)

3. I tell people over and over again - Work out what you need to do in a flow chart, it takes a few minutes, make an input output table, translating that flow chart and table into whatever programming language is relatively trivial.

Ill add the flow chart fro what your doing under this - SEE it's trivial but NOT useless.

Fit your Input outputs round it and you should be able to see what you need to do.

https://www.instructables.com/id/Starting-programmi...

prog.jpg