/*
'C' : Prints all the commands and their functions.
'P' : Rotates the motor in positive (CW) direction, relative.
'N' : Rotates the motor in negative (CCW) direction, relative.
'R' : Rotates the motor to an absolute positive position (+).
'r' : Rotates the motor to an absolute negative position (-).
'S' : Stops the motor immediately.
'A' : Sets an acceleration value.
'L' : Prints the current position/location of the motor.
'H' : Goes back to 0 position from the current position (homing).
'U' : Updates the position current position and makes it as the new 0 position.
*/
#include <IRremote.h>
#include <AccelStepper.h>
//#include <TM1650.h>
#include <TM1637.h>
#include <EEPROM.h>
//#include <Stepper.h>
#define PIN_RECEIVER 2 // Signal Pin of IR receiver
#define ENCODER_CLK 3
#define ENCODER_DT 6//#define ENCODER_SW 4
IRrecv receiver(PIN_RECEIVER);
#define BUTTON_PIN 7
TM1637 TM;
int dir=0;
int dir1;
int c=0;
long g=0;
char gt[5];
char x[5];
char ee[5];
char y[5];
int counter;
#define M1_STEP 11
#define M1_DIR 10
#define M1_ENA 12
#define MO 8
long receivedSteps = 0; //Number of steps
long receivedSpeed = 0; //Steps / second
long receivedAcceleration = 0; //Steps / second^2
char receivedCommand;
//-------------------------------------------------------------------------------
int directionMultiplier = 1; // = 1: positive direction, = -1: negative direction
bool newData, runallowed = false; // booleans for new data from serial, and runallowed flag
AccelStepper stepper(1, 10, 11);// direction Digital 9 (CCW), pulses Digital 8 (CLK)
void setup()
{
pinMode(MO,OUTPUT);
pinMode(BUTTON_PIN,INPUT);
pinMode(M1_ENA,OUTPUT);
pinMode(M1_STEP,OUTPUT);
pinMode(M1_DIR,OUTPUT);
EEPROM.put(100,x); //used address : 100
EEPROM.get(0,y);
digitalWrite(MO,LOW);
Serial.begin(9600);
TM.begin(4, 5, 4); // clockpin, datapin, #digits
TM.displayClear();
// TM.displayOn();
TM.setBrightness(7); // full brightness, default is 3
TM.displayPChar(y);
TM.displayPChar(EEPROM.get(0,y));
stepper.setMaxSpeed(400); //SPEED = Steps / second
stepper.setAcceleration(800); //ACCELERATION = Steps /(second)^2
stepper.disableOutputs(); //disable outputs
receiver.enableIRIn(); // Start the receiver
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
// pinMode(interruptPin, INPUT_PULLUP);
//attachInterrupt(digitalPinToInterrupt(BUTTON_PIN), stopm1,FALLING);
// attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
//int dtValue = digitalRead(ENCODER_DT);
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
}
if (dtValue == LOW) {
counter--; // Counterclockwise
}
char yy[5];
sprintf(yy,"%04d",getCounter());
TM.displayPChar(yy);
//x[0]=yy ;
//EEPROM.update(0,x);
sprintf(y,"%04d",EEPROM.get(0,x));
Serial.println(counter);
}
int getCounter() {
int result;
noInterrupts();
result = counter;
interrupts();
return result;
}
void stopm1(){
// noInterrupts();
Serial.println("stop");
g=counter;
char yy[5];
sprintf(yy,"%04d",getCounter());
TM.displayPChar(yy);
// interrupts();
}
void m1stepR() {//if (receiver.decodedIRData.command==168)
digitalWrite(MO,HIGH);
delay(300);
while(counter < g ){
digitalWrite(M1_STEP,HIGH);
digitalWrite(M1_DIR,HIGH);//dir==1? :LOW);
delayMicroseconds(500);
digitalWrite(M1_ENA,LOW);
delayMicroseconds(500);
digitalWrite(M1_STEP,LOW);
}
delay(500);
digitalWrite(MO,LOW);
}
void m1stepL() {//if (g>counter){}
digitalWrite(MO,HIGH);
delay(300);
while( counter > g){
digitalWrite(M1_STEP,HIGH);
digitalWrite(M1_DIR,LOW);
delayMicroseconds(500);
digitalWrite(M1_ENA,LOW);
delayMicroseconds(500);
digitalWrite(M1_STEP,LOW);
}
delay(500);
digitalWrite(MO,LOW); }
void resetCounter() {
noInterrupts();
counter = 0;
EEPROM.update(100,x);
Serial.println(EEPROM.get(100,y));
interrupts();
}
void rest(){
g=g*0;
char text="0000";
sprintf(text,"%04d",x);
TM.displayClear();
TM.displayPChar("hloo");
}
void loop()
{
if (receiver.decode()) { // Checks received an IR signal
delay(50);
translateIR();
receiver.resume(); // Receive the next value
}
}
void translateIR()
{// receivedCommand = Serial.read(); pass the value to the receivedCommad variable
//indicate that there is a new data by setting this bool to true
newData = true;
if (newData == true) //we only enter this long switch-case statement if there is a new command from the computer
{
// Takes command based on IR code received
switch (receiver.decodedIRData.command) {
case 162:
noInterrupts();
stepper.stop(); //stop motor
stepper.disableOutputs(); //disable power
Serial.println("Stopped."); //print action
runallowed = false; //disable running
dir1=0;
lcdPrint("POWER");
interrupts();
break;
case 226:
g=g*0;
TM.displayClear();
//TM.displayPChar("---0");
break;
case 43:
c = '000';
lcdPrint(c);//"TEST"
break;
case 2:
lcdPrint("PLUS");
receivedSteps = (g*10); //value for the steps
receivedSpeed = 200; //value for the speed
directionMultiplier = 1; //We define the direction
Serial.println("Absolute position (+)."); //print the action
RotateAbsolute(); //Run the function
break;
case 194:
lcdPrint("BACK");
break;
case 224:
lcdPrint("PREV.");
g=counter;
g=g-1;
TMPrint();
// m1stepL();
break;
case 168:
dir1=0;
lcdPrint("PLAY");
if (receiver.decodedIRData.command==168){
//m1stepL();
}
if (receiver.decodedIRData.command==168){
// m1stepR();
}
Serial.println(g);
Serial.println(counter);
break;
case 144:
lcdPrint("NEXT");
g=counter;
g=g+1;
TMPrint();
// m1stepR();
break;
case 152:
lcdPrint("MINUS");
break;
case 176:
g=(g*0);
Serial.println(g);
rest();
TM.displayPChar("0000");
break;
case 104:
g=g*10;
c = '0';
Serial.println(g);
lcdPrint(c);
break;
case 48:
g=(g*10)+1;
c = '1';
Serial.println(g);
lcdPrint(c);
break;
case 24:
g=(g*10)+2;
c = '2';
Serial.println(g);
lcdPrint(c);
break;
case 122:
g=(g*10)+3;
c = '3';
Serial.println(g);
lcdPrint(c);
break;
case 16:
g=(g*10)+4;
c = '4';
Serial.println(g);
lcdPrint(c);
break;
case 56:
g=(g*10)+5;
c = '5';
Serial.println(g);
lcdPrint(c);
break;
case 90:
g=(g*10)+6;
c = '6';
Serial.println(g);
lcdPrint(c);
break;
case 66:
g=(g*10)+7;
c = '7';
Serial.println(g);
lcdPrint(c);
break;
case 74:
g=(g*10)+8;
c = '8';
Serial.println(g);
lcdPrint(c);
break;
case 82:
g=(g*10)+9;
c = '9';
Serial.println(g);
lcdPrint(c);
break;
default:
Serial.print(receiver.decodedIRData.command);
Serial.print(" other button");
}}
if(g>1010){
rest();
Serial.println(g);
TM.displayPChar("EROR");
Serial.println("eror");
}
}
void TMPrint(){
char gt[5];
sprintf(gt,"%04d",g);
TM.displayPChar(gt);
}
void RotateAbsolute()
{
//We move to an absolute position.
//The AccelStepper library keeps track of the position.
//The direction is determined by the multiplier (+1 or -1)
//Why do we need negative numbers? - If you drive a threaded rod and the zero position is in the middle of the rod...
runallowed = true; //allow running - this allows entering the RunTheMotor() function.
stepper.setMaxSpeed(receivedSpeed); //set speed
stepper.moveTo(directionMultiplier * receivedSteps); //set relative distance
}
void lcdPrint(char* text)
{
TMPrint();
Serial.println(text);
Serial.println(receiver.decodedIRData.command);
}