Simon’s Diagnostics

An Alternate Diagnostics sketch that heavily uses the Serial output window.

#include <Servo.h> 

/* Rotary encoder read example */
#define ENCODER_A 14
#define ENCODER_B 15
#define ENCODER_PORT PINC
#define SWITCH 13
#define BUTTON 12
#define RGB_RED 11
#define RGB_GREEN 10
#define RGB_BLUE 9
#define LED 6
#define SERVO 5
#define PIEZO 3
#define RELAY 2
#define POT 2
#define HALL 3
#define THERMISTOR 4
#define PHOTOCELL 5


Servo myservo;  // create servo object to control a servo 

void setup()
{
 /* Setup encoder pins as inputs */
 pinMode(ENCODER_A, INPUT);
 digitalWrite(ENCODER_A, HIGH);
 pinMode(ENCODER_B, INPUT);
 digitalWrite(ENCODER_B, HIGH);
 pinMode(SWITCH, INPUT);
 pinMode(BUTTON, INPUT);
 pinMode(RGB_RED, OUTPUT);
 pinMode(RGB_BLUE, OUTPUT);
 pinMode(RGB_GREEN, OUTPUT);
 pinMode(LED, OUTPUT);
 pinMode(PIEZO, OUTPUT);
 pinMode(RELAY, OUTPUT);

 Serial.begin (115200);
  Serial.println("");
  Serial.println("");
 Serial.println("Begin diagnostic");
}

void loop() {
  testDigitalIn("Switch", SWITCH);
  testDigitalIn("Button", BUTTON);
  
  testDigitalOut("LED", LED);
  testDigitalOut("Red", RGB_RED);
  testDigitalOut("Green", RGB_GREEN);
  testDigitalOut("Blue", RGB_BLUE);
  testAnalogOut("Piezo", PIEZO);
  testDigitalOut("Relay", RELAY);

  testServo();
  
  testAnalogIn("Light Sensor", PHOTOCELL);
  testAnalogIn("Hall Sensor", HALL);
  testAnalogIn("Thermistor", THERMISTOR);
  testAnalogIn("Potentiometer", POT);
  
  testRotary();
}

void testDigitalOut(String name, int pin) {
  Serial.println("Testing: " + name);
  digitalWrite(pin, HIGH);
  delay(800);
  digitalWrite(pin, LOW);
  delay(200);
}

void testAnalogOut(String name, int pin) {
  Serial.println("Testing: " + name);
  analogWrite(pin, 128);
  delay(800);
  digitalWrite(pin, LOW);
  delay(200);
}  

void testServo() {
  myservo.attach(SERVO);
  delay(100);
  Serial.println("Testing: Servo");
  myservo.write(45);
  delay(700);
  myservo.write(135);
  delay(700);
  myservo.detach(); 
}

void testDigitalIn(String name, int pin) {
  int startValue = digitalRead(pin);
  int newValue = startValue;
  Serial.println("Testing: " + name + ". Change value to continue...");
  while (startValue == newValue) {
   newValue = digitalRead(pin);
   delay(40);
  }
  analogWrite(PIEZO, 128);
  delay(100);
  digitalWrite(PIEZO, LOW);
  delay(200);
}

void testAnalogIn(String name, int pin) {
  int startValue = analogRead(pin);
  int delta = 0;
  Serial.println("Testing: " + name + ". Change value to continue...");
  while (delta < 20) {
   delta = abs(startValue - analogRead(pin));
   delay(40);
  }
  analogWrite(PIEZO, 128);
  delay(100);
  digitalWrite(PIEZO, LOW);
}

void testRotary() {
  Serial.println("Testing: Rotary Encoder. Turn 3 clicks to continue...");
  int8_t startData = 0;
  while (abs(startData) < 12) {
    startData += read_encoder();
  }
  analogWrite(PIEZO, 128);
  delay(100);
  digitalWrite(PIEZO, LOW);
}

int8_t read_encoder()
{
    static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
    static uint8_t old_AB = 0;
    old_AB <<= 2;                   //remember previous state
    old_AB |= ( ENCODER_PORT & 0x03 );  //add current state
    return ( enc_states[( old_AB & 0x0f )]);
}

									

Leave a Reply

Your email is never published nor shared.

You may use these HTML tags and attributes:<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>