hello,
i am making a custom code scanner using 2 rows of black and white the bottom row is black white black white etc it is 8 bit so 4 white 4 black. to indicate the upper row so if the bottom rows sees the upper row is changing, it converts the input into binary like: black white white white white white white white will be 1. i use 2 line tracking sensors for this im also using a servo motor to make the codes circle so the scanner can read them. what im trying to put in rigth now is that if the maxnum wich is 8 including 0 == a random number between 0-7 it stops the motor waits 3 seconds and returns to his routine. i have already added that it stops at a random number between 1-3. but i want to make it more accurate so it stops between 0-7 of the bottom row here is my code:
#include <Wire.h> //used for the lcd
#include <LiquidCrystal_I2C.h>
#include <Time.h>
#define TIME_MSG_LEN 11
#define TIME_HEADER 'T'
#define TIME_REQUEST 7
int oldval=1; //used to indicate if the upper row changed
int count=0; //changes everytime the bottom row changes and increases once it reached maxnum wich is 8 it resets itself
int val = 1; //used to calculate from binary to decimal
int maxnum=8; //the max number of counts that the scanner does before it resets itself
int pos = 0; //used to calculate from binary to decimal
int scans = 0; //the variable that changes everytime it scanned one code it shows the amount of scans he did
int E2 = 9; //for the servo motor
int M2 = 10; //for the servo motor
//int change = 0;
long randNumber; //the random number for the upper row the binary code converted to decimal with a max of 3 and a min of 1
long randCount; //the random number for the bottom row from 0-7
//int displayval = 0;
LiquidCrystal_I2C lcd(0x27,20,4); //for the lcd to work
void setup()
{
Serial.begin(9600);
int ledPin = 8; // changing pin 8 to 5v
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH);
int ledPin2 = 7; // changing pin 7 to 5v
pinMode(ledPin2, OUTPUT);
digitalWrite(ledPin2, HIGH);
int ledPin3 = 4; // changing pin 4 to 5v
pinMode(ledPin3, OUTPUT);
digitalWrite(ledPin3, HIGH);
attachInterrupt(0, Poort, CHANGE); //the attachinterrupt to see if the bottom row changes so it knows to look at the upper row
lcd.init(); //to keep the lcd on
lcd.backlight(); //to keep the backlight of the lcd on
randNumber = random(1, 4); //generates a random number for the randNumb to stop (this works)
analogWrite (E2,75); //to keep the servo motor running to circle the codes
digitalWrite(M2,HIGH);
lcd.setCursor(15,3); //is for the display don't need to look at this
lcd.print(randNumber);
lcd.setCursor(13,2);
lcd.print("Dest:");
lcd.setCursor(0,0);
lcd.print("Aantal Gescanned:");
lcd.setCursor(0,1);
lcd.print(scans);
lcd.setCursor(0,2);
lcd.print("Waarde: ");
}
void loop(){
if (count == 7) //if the count reaches 7 it updates the lcd
{
lcd.setCursor(0,3);
lcd.print(pos);
lcd.print(" ");
lcd.setCursor(0,1);
}
if (pos == randNumber) // if the decimal number wich is converted from decimal in the interrupt == the random number generated in the setup it slows down the servo to be more accurate at his stop
{
analogWrite (E2,110);
lcd.setCursor(15,3); //for the lcd
lcd.print(randNumber);
lcd.print(scans++); //adds a scan everytime it passes this
randNumber = random(1, 4)
if (randCount == count) //if the random nummer generated for the bottom row == to the the count wich counts every square in the bottom row it stops the servo generates a new randomnumber waits 3 seconds generates a new random number for the count
{
analogWrite (E2, 200);
randNumber = random(1, 4);
delay(3000);
randCount = random(0, 7);
analogWrite (E2, 75); //after the 3 seconds it goes back to normal speed
}
}
}
void Poort(){ //the interrupt where i build in a debug system to see what it does and if it does what i want
Serial.print(count);
Serial.print(" - ");
Serial.print(digitalRead(2));
Serial.print(" - ");
Serial.print(digitalRead(3));
Serial.print(" - ");
Serial.print(randCount);
Serial.print(" - ");
Serial.print(randNumber);
Serial.print(" - ");
count++; //everytime it changes it adds one count for the blocks
pos = pos + (val * (1-digitalRead(3))); //calculates binary to decimal pin 3 is the sensor for the upper row
val = val * 2; //calculates binary to decimal
Serial.print(pos); //after it calculated it it outputs the decimal into to monitor for me to check it
Serial.println(" ");
if (count == maxnum) //once the count changed 8 times it resets the variables to startover
{
count = 0;
pos = 0;
val = 1;
}
}
i have no clue how to fix it. right now when i run it it sometimes work. i think it may have to do with the loop and the interrupt but i don't know how to fix this.
the lcd.print is just to make it easier to read when im next to the arduino.
right now it doesnt slow down and it stops at one point in the code only. i dont think he uses the randCount
this is the what i see in my monitor.
first row: the count counts to 8 everytime in order to get a 8 bit binary code (works)
second row: the bottom row to indicate if the upper has to change aswell (works)
third row: the upper row shows a 0 for black square and 1 for a white square (works)
fourth row: shows the randCount a random nummer generated used to make it stop at a place in the count (does not work)
fifth row: shows the randNumb a number generated to stop at a certain code after the code is calculated wich is after 8 squares (works)
sixth row: shows the number calculated from the binary code (works)
0 - 0 - 0 - 0 - 2 - 1
1 - 1 - 1 - 0 - 2 - 1
2 - 0 - 1 - 0 - 2 - 1
3 - 1 - 1 - 0 - 2 - 1
4 - 0 - 1 - 0 - 2 - 1
5 - 1 - 1 - 0 - 2 - 1
6 - 0 - 1 - 0 - 2 - 1
7 - 1 - 1 - 0 - 2 - 1
0 - 0 - 0 - 0 - 2 - 1
1 - 1 - 0 - 0 - 2 - 3
2 - 0 - 1 - 0 - 2 - 3
3 - 1 - 1 - 0 - 2 - 3
4 - 0 - 1 - 0 - 2 - 3
5 - 1 - 1 - 0 - 2 - 3
6 - 0 - 1 - 0 - 2 - 3
7 - 1 - 1 - 0 - 2 - 3
0 - 0 - 0 - 0 - 2 - 1
1 - 1 - 1 - 0 - 2 - 1
2 - 0 - 1 - 0 - 2 - 1
3 - 1 - 1 - 0 - 2 - 1
4 - 0 - 1 - 0 - 2 - 1
5 - 1 - 1 - 0 - 2 - 1
6 - 0 - 1 - 0 - 2 - 1
7 - 1 - 1 - 0 - 2 - 1
0 - 0 - 1 - 0 - 2 - 0
1 - 1 - 0 - 0 - 2 - 2
2 - 0 - 1 - 2 - 3 - 2
3 - 1 - 1 - 6 - 3 - 2
4 - 0 - 1 - 6 - 3 - 2
5 - 1 - 1 - 6 - 3 - 2
6 - 0 - 1 - 6 - 3 - 2
7 - 1 - 1 - 6 - 3 - 2