Instantly share code, notes, and snippets.

Embed
What would you like to do?
Seidenstrasse arduino router dirty 1
#include <Bounce2.h>
#define RUNSTOP_DELAY 200 // Delay in ms, before runstops will be active after motor command
#define RUNSTOP_LEFT_PIN 12 // pull-up on 13 will not work
#define RUNSTOP_RIGHT_PIN 11
#define RUNSTOP_CENTER_PIN 10
#define BUTTON_RIGHT_PIN 9
#define BUTTON_LEFT_PIN 8
// indices for input array, moar readability
#define RUNSTOP_LEFT 0
#define RUNSTOP_RIGHT 1
#define RUNSTOP_CENTER 2
#define BUTTON_RIGHT 3
#define BUTTON_LEFT 4
#define DEBOUNCE_VALUE 10 // 10 ms
const int out_relais4 = 4;
const int out_relais5 = 5;
const int out_relais6 = 6;
const int out_relais7 = 7;
byte inputs[] = {RUNSTOP_LEFT_PIN,
RUNSTOP_RIGHT_PIN,
RUNSTOP_CENTER_PIN,
BUTTON_RIGHT_PIN,
BUTTON_LEFT_PIN};
#define NUMINPUTS sizeof(inputs)
Bounce debouncer[NUMINPUTS] = Bounce();
void stopMotor()
{
int i;
// 4,5,6,7 high -> Motor stop
Serial.println("StopMotor(): Setting 4,5,6,7 -> high");
for(i = out_relais4; i <= out_relais7; ++i)
{
digitalWrite(i, HIGH);
}
}
void turnRight()
{
digitalWrite(out_relais4, LOW);
digitalWrite(out_relais5, LOW);
digitalWrite(out_relais6, HIGH);
digitalWrite(out_relais7, HIGH);
// wait here to block processing of runstops while still pressed
// maybe 50ms?
delay(RUNSTOP_DELAY);
}
void turnLeft()
{
digitalWrite(out_relais4, LOW);
digitalWrite(out_relais5, LOW);
digitalWrite(out_relais6, LOW);
digitalWrite(out_relais7, LOW);
delay(RUNSTOP_DELAY);
}
void setup(void)
{
byte i;
for(i = out_relais4; i <= out_relais7; ++i)
{
pinMode(i, OUTPUT);
}
stopMotor();
for (i = 0; i < NUMINPUTS; ++i)
{
pinMode(inputs[i], INPUT_PULLUP);
debouncer[i].attach(inputs[i]);
debouncer[i].interval(DEBOUNCE_VALUE); // 10ms
}
Serial.begin(9600);
Serial.println("Seidenstrasse Router\n");
}
void loop(void) {
int input_values[NUMINPUTS];
for (int i = 0; i < NUMINPUTS; ++i)
{
debouncer[i].update();
input_values[i] = debouncer[i].read();
}
if(input_values[RUNSTOP_CENTER] == LOW ||
input_values[RUNSTOP_LEFT] == LOW ||
input_values[RUNSTOP_RIGHT] == LOW)
{
// TODO: * dont stop again if direction change requested < 50ms
Serial.println("runstop LOW!");
// stop motor
stopMotor();
}
if(input_values[BUTTON_RIGHT] == LOW)
{
Serial.println("Turning right!");
turnRight();
// after time X move back to center position
}
if(input_values[BUTTON_LEFT] == LOW)
{
Serial.println("Turning left!");
turnLeft();
// after time X move back to center position
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment