Skip to content

Instantly share code, notes, and snippets.

@mithi
Last active November 27, 2015 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mithi/09ae419a3c7eba285221 to your computer and use it in GitHub Desktop.
Save mithi/09ae419a3c7eba285221 to your computer and use it in GitHub Desktop.
Weird Problem with the Arduino - it seems that there is a problem using pin 11 and 13 together but if I use a different pwm pin like pin 5. It's okay.
#include "SimplestLibrary.h"
Sweeper sweeper;
Buzzer buzzer;
AnalogLED led;
int f1 = 3000;
int f2 = 4500;
int f = 0;
void setup() {
sweeper.New(f1, f2, BACKANDFORTH);
buzzer.New(13);
led.New(11);
}
void loop() {
f = sweeper.Update(1);
buzzer.Play(f, 4, 2);
led.NewBrightness(map(f, f1, f2, 0, 50));
delay(1);
}
#include "SimplestLibrary.h"
/**********************************
* ANALOG LED
**********************************/
void AnalogLED::New(int p){
_pin = p;
pinMode(_pin, OUTPUT);
}
void AnalogLED::NewBrightness(int b){
analogWrite(_pin, b);
}
/**********************************
* SWEEPER
**********************************/
void Sweeper::New(int s, int e, byte x){
if(s < e ){
_dir = FORWARD;
_start = s;
_end = e;
}else{
_dir = REVERSE;
_start = e;
_end = s;
}
_sweepsMade = 0;
_type = x;
_state = s;
}
int Sweeper::Update(bool itsTime){
if(itsTime){
if(_type == NORMAL){
_dir == FORWARD ? _forward() : _reverse();
}else{
_backAndForth();
}
}
return _state;
}
unsigned long Sweeper::SweepsMade(){
return _type == NORMAL ? _sweepsMade : _sweepsMade / 2;
}
void Sweeper::_forward(){
_state++;
if(_state > _end){
_state = _start;
_sweepsMade++;
}
}
void Sweeper::_reverse(){
_state--;
if(_state < _start){
_state = _end;
_sweepsMade++;
}
}
void Sweeper::_backAndForth(){
if (_dir == FORWARD){
if(_state < _end){
_state++;
}else{
_dir = REVERSE;
_state--;
_sweepsMade++;
}
}else{ //reverse
if(_state > _start){
_state--;
}else{
_dir = FORWARD;
_state++;
_sweepsMade++;
}
}
}
/**********************************
* BUZZER
**********************************/
void Buzzer::New(int p){
_pin = p;
pinMode(_pin, OUTPUT);
}
void Buzzer::Play(int pitch, float period, int d){
tone(_pin, pitch, period);
delay(d);
}
#include <Arduino.h>
/********************************** CONSTANTS **********************************/
const bool BACKANDFORTH = 0;
const bool NORMAL = 1;
const bool FORWARD = 0;
const bool REVERSE = 1;
/********************************** CLASSES **********************************/
class AnalogLED{
int _pin;
int _brightness;
public:
void New(int p);
void NewBrightness(int b);
};
class Sweeper{
bool _dir;
bool _type;
int _state;
int _start;
int _end;
unsigned long _sweepsMade;
public:
void New(int s, int e, byte x);
int Update(bool itsTime);
unsigned long SweepsMade();
private:
void _forward();
void _reverse();
void _backAndForth();
};
class Buzzer{
int _pin;
public:
void New(int p);
void Play(int freq, float period, int d);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment