Quote:
compiled it comes to <4kb
What about SRAM? How much does your Sketch need?
Quote:
ATMEGA48V-10PI
Does the Sketch work correctly at 8 MHz?
I assume sram is the memory for variables.
(globals)
2 byte
3 Boolean
2 char
setup():
1 byte
analogToSerial(1 byte):
1 byte
1 int
Motor(2 byte):
2 boolean
serialCommand(1 byte):
2 byte
AlignFrame()
1 byte
1 int
loop()
5 byte
1 int
Program flow:
loop calls
1)serialCommand which calls
a)analogToSerial
b)Motor
2)AlignFrame(which calls motor)
I do not know if it runs at 8MHz. the only timing issue would be the Serial communication.
Full Code:
/*
Digital Analog
0 Serial * 0 Current1
1 Serial * 1 Current2
2 serial select 2 Led1
3 Input Selection(Done) 3 Led2
4 In1A 4 Postion
5 In2A 5
6 In1B
7 In2B
8
9 Stby(done)
10 Pwm(Done)
11 PosA
12 PosB
13 PosC
*/
byte mtrSpeed=128;
boolean standBy=false;
boolean Stall[2]={false,false};
#define StartChar 'f'
static char address = '0';
boolean align=false;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
pinMode(2, OUTPUT); //serial mode select
pinMode(3, OUTPUT); //set LDR selector to ouput mode
pinMode(4, OUTPUT); //motor directional control
pinMode(5, OUTPUT); //motor directional control
pinMode(6, OUTPUT); //motor directional control
pinMode(7, OUTPUT); //motor directional control
pinMode(9, OUTPUT); //set standby to ouput mode
pinMode(10, OUTPUT); //set Motor PWM selector to ouput mode
pinMode(11, INPUT); //AddressA
pinMode(12, INPUT); //AddressB
pinMode(13, INPUT); //AddressC
byte mod=0;
for(byte i=13;i>=11;i--){
mod = mod * 2;
if( digitalRead(i)){
mod = mod + 1;
}
}
/*
byte mod = digitalRead(11) ;
if(digitalRead(12)){ mod += 2; }
if(digitalRead(13)){ mod += 4;}
*/
address = mod + address;
}
void analogToSerial(byte number){
byte NewNum= number -48;
//0-currentA (0)
//1-currentB (1)
//2-PostionA (2)
//3-PostionB (3)
//4-Ldr1 (4)(pin D13 low)
//5-Ldr2 (5)(pin D13 low)
//6-Ldr3 (4)(pin D13 High)
//7-Ldr4 (5)(pin D13 High)
//8- MotorSpeed
int inpute;
if(NewNum ==8){
inpute = mtrSpeed;
}else{
if(NewNum > 5){
digitalWrite(3,HIGH);
NewNum = NewNum - 2;
}else if(NewNum > 3 ){
digitalWrite(3,LOW);
}
inpute = analogRead(NewNum)/4;
if(NewNum < 2){
if(Stall[number]){
inpute = 255;
}
}
}
byte chrOut = inpute;
digitalWrite(2,HIGH);
Serial.print(StartChar);
Serial.print(0);
Serial.print(inpute,BYTE);
Serial.print(StartChar);
digitalWrite(2,LOW);
}
void Motor(byte motor,byte clockwise){
if(Stall[motor]){
delay(10);
}
Stall[motor] = false;
digitalWrite(9,standBy);
analogWrite(10,mtrSpeed); //set speed control
boolean DirA=false;
boolean DirB=false;
if(clockwise == 0){ DirA=true; }
if(clockwise == 1){ DirB=true; }
if(DirA){
digitalWrite(4+motor*2,HIGH);
}else{
digitalWrite(4+motor*2,LOW);
}
if(DirB){
digitalWrite(4+motor*2,HIGH);
}else{
digitalWrite(4+motor*2,LOW);
}
if(analogRead(motor) > 100){ //Stall Sensor
Stall[motor] = true;
digitalWrite(4+motor*2,LOW);
digitalWrite(4+motor*2,LOW);
}
}
void serialCommand(byte command){
if (command >= '0' && command <= '8'){
analogToSerial(command);
}else if (command >= 'A' && command <= 'B'){
byte dir = command-65;
byte motor=0;
if(dir > 3){
motor =1;
dir=dir-3;
}
Motor(motor,dir);
}else{
switch(command){
case 'd':
if(mtrSpeed > 5){ mtrSpeed -= 5; }else { mtrSpeed =0; }
break;
case 'u':
if(mtrSpeed < 250){ mtrSpeed += 5; }else { mtrSpeed =255; }
break;
case 'h': //halt
standBy=false;
digitalWrite(9,standBy);
break;
case 's': //go
standBy=true;
case 'a':
align = true;
case 'b':
align = false;
}
}
}
void AlignFrame(){
for(byte i=0;i<2;i++){
digitalWrite(3,i);
int dif = abs(analogRead(5)-analogRead(4));
dif = dif/dif+1;
Motor(i,dif);
}
}
void loop() {
int i=0;
byte srByte[4] = {0,0,0,0}; // for incoming serial data
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
for(byte j=0;j<=3;j++){
srByte[j] = Serial.read();
while (Serial.available() == 0 || i>1000 ) {
i++;
}
}
if(srByte[0]==StartChar){
if(srByte[1]==address ){
if(srByte[3]==StartChar ){
serialCommand(srByte[2]);
}
}
}
Serial.flush();
}
if(align){
AlignFrame();
}
}