Skip to content

Instantly share code, notes, and snippets.

@XANOZOID
Last active October 19, 2017 23:40
Show Gist options
  • Select an option

  • Save XANOZOID/31b9e32aac4c044a0f9d683ff6dd49da to your computer and use it in GitHub Desktop.

Select an option

Save XANOZOID/31b9e32aac4c044a0f9d683ff6dd49da to your computer and use it in GitHub Desktop.
#include <IOShieldOled.h>
const int ledPin = 13;
const int sensorPin = A0;
int BUTTON[] = { 4, 78 };
int SWITCH[] = { 2, 7, 8, 79 };
int accel = 0;
int velocity = 0;
int postLine = 0;
boolean cruiseOn = false;
// Fool coach that we doing work
void blink_task(int id, void*_){ digitalWrite(ledPin, !digitalRead(ledPin) ); }
void setup() {
pinMode( ledPin, OUTPUT);
createTask(blink_task, (1000/5), TASK_ENABLE, 0);
for( int i = 0; i < 2; i ++ ) pinMode( BUTTON[i], INPUT); // Buttons
for( int i = 0; i < 4; i ++ ) pinMode( SWITCH[i], INPUT ); // Switches
IOShieldOled.begin();
IOShieldOled.displayOn(); // Shield
}
void post( String message ){
char strArr[50];
message.toCharArray( strArr, 50 );
IOShieldOled.setCursor( 0, postLine ++ );
IOShieldOled.putString( strArr );
if( postLine >= ( 3 + cruiseOn ) ) postLine = 0;
}
void loop(){
IOShieldOled.clearBuffer();
cruiseOn = digitalRead(SWITCH[3]) == HIGH;
if( cruiseOn ){
post("cruise control");
int sensorValue = ((float)analogRead(sensorPin)/1023.0) * 80;
int diff = ( velocity - sensorValue );
accel = ( diff != 0 )*( 1 - (diff>0)*2 );
post( "cruise vel:" + String(sensorValue) );
} else {
post("drunk driving");
accel = (digitalRead(BUTTON[0])|digitalRead(SWITCH[0] ))*2 - 1;
}
velocity += accel;
velocity = (velocity<0)? 0: velocity;
post( "acc:" + String(accel));
post( "vel:" + String(velocity) );
IOShieldOled.updateDisplay();
delay( 30 );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment