'set' not declared? Adruino - processing

I have zero experience with programming adruino but I have to test these robots for my job. I was told this code drives the robots foreword. When I run this code I get the error that 'set' is not declared in this scope. Help? Or let me know if this isn't even the right question to ask. Those libraries at the top are also with me, but I'm unsure if I need to post them to solve this particular problem.
#include <Lobotank.h>
#include <tank_Cwrap.h>
int temp_R=0;
int temp_L=0;
int c15=0;
void setup()
{
enableDebug();
//test sensors(1000);
set speed(125);
}
void loop()
{
update sensors();
int pattern = 0;
long rndm = random(0,10);
serial.println(rndm);
//serial.println(temp_L);
//serial.println(lf_left);
//serial.println(lf_mleft);
//serial.println(lf_mright);
//serial.println(lf_right);
if (lf_left>= 500)
pattern += 8;
if (lf_mleft >= 500)
pattern += 4;
if (lf_mright >= 500)
pattern += 2;
if (lf_right >= 500)
pattern += 1;
switch (pattern)
{
case 0:
if (temp_R ==1)
turnRight_hard();
else
turnAround_left()
break;
case 1:
turnRight_slight();
temp_R = 1;
break;
case 2:
turnRight_slight();
temp_R = 1;
break;
case 3:
delay(25);
turnRight_slight();
break;
case 6:
forward();
temp_R = 0;
c15 = 0;
break;
case 7: //turn right
turnRight_hard();
temp_R = 1;
break;
case 8:
turnleft_slight();
temp_R = 0;
break;
case 12:
delay(15);
turnLeft_slight();
break;
case 14: //turn left
turnLeft_hard();
break;
case 15:
delay(25);
if (rndm <= 5 && c15 <= 3)
turnleft_hard();
else
{
if (rndm >= 6 && c15 <= 3)
turnRight_hard();
else
{
if (c15 >= 5)
turnRight_hard();
else
{
if (c15>= 10)
stop();
}
}
}
c15++;
break;
}

This doesn't look like valid syntax:
set speed(125);
This looks like a function call, but functions can't have spaces in them. Maybe you meant one of these:
setspeed(125);
setSpeed(125);
set_speed(125);
Look in those .h files for a function similar to these, and make sure you call it with the correct name.

Related

Stack Data structures (infix to postfix)

This is a program to convert infix to postfix in stack data structures.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int F(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 2;
case '*':
case '/':return 4;
case '^':
case '$':return 5;
case '(':return 0;
case '#':return -1;
default:return 8;
}
}
int G(char symbol)
{
switch(symbol)
{
case '+':
case '-':return 1;
case '*':
case '/':return 3;
case '^':
case '$':return 6;
case '(':return 9;
case ')':return 0;
default:return 7;
}
}
void infixtopostfix(char infix[],char postfix[])
{
int top,i,j=0;
top = -1;
char s[30],symbol;
s[++top]= '#';
for(i=0;i<strlen(infix);i++)
{
symbol = infix[i];
while(F(s[top]) > G(symbol))
{
postfix[j]=s[top--];
j++;
}
if(F(s[top]) != G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++]=s[top--];
}
postfix[j] = '\0';
}
int main()
{
char infix[20],postfix[20];
printf("Enter the infix expression:\n");
scanf("%s",infix);
infixtopostfix(infix,postfix);
printf("Postfix Expression is %s",postfix);
return 0;
}
In this code, what is going on with the following lines?
if(F(s[top]) != G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top] != '#')
{
postfix[j++]=s[top--];
}
I don’t understand how f(s[top]) != g(symbol) is different than f(s[top]) > g(symbol), because if it is greater, it means automatically it is not equal. And what is f(s[top]) and g(symbol)?
The conditions
f(s[top]) != g(symbol)
and
f(s[top]) > g(symbol)
are both different.
The first one gives false if f(s[top]) and g(symbol) are equal. But in the second condition if f(s[top]) is less than g(symbol) then it generates false. But according to the first condition, true should be generated.

Microchip C18 send data to Terminal as numeric

I am working on a project on which i need to send data over USART to terminal.
I need to display the data as the numeric value (0-255) of the char (which collected from the EEPROM
i have managed to send the char as is to the terminal (using Putty or TerMite)
My problem starts where the value of the char is non-printable
That's why i will need to convert the value of the char to numeric
Example: when the data acquired from the EEPROM is 0x31 my routine will send '1' but i will need to send '049' or '49' to the terminal
void SendToSer(void) {
unsigned char Looper;
for (Looper=EEPROM_START;Looper<EEPROM_END;Looper++){
ReadEEPROM(Looper); //returns ReadResult
Write1USART((char) ReadResult); //Sends the ASCII
ClrWdt();
}
}
Thanks,
The sprintf as jolati suggested can be a good work horse in some situations or, even better, snprintf if it is available for your version of C18. Both routines follow the standard printf formatting (ex. https://en.wikipedia.org/wiki/Printf_format_string).
#include "stdio.h"
void main() {
char buffer[80];
unsigned char len, number = 152;
// Write at most 80 bytes to our buffer
len = snprintf(buffer, 80, "sprintf string, heres a number: %d", number);
// buffer now contains our string, len is the number of bytes written
// or
len = printf(buffer, "... %d",number);
}
Thanks , I've decided to take it the long way......
Here's what i've done is:
Manipulated the int value of the char to 3 new char
(i.e.) 243 became 3 chars - 50,52,51 (the ASCII of the digits)
(maybe its long and lame but it works like a charm)
heres the script....
void ConvertToNumeric(unsigned char IsValue, unsigned int LineNumber){
unsigned int SourceInt;
ClrWdt();
if (IsValue == 1){
SourceInt = (int) ReadResult;
}else{
SourceInt = (int) LineNumber;
LineNumber++;
}
ClrWdt();
switch (SourceInt/100){
case 2 : FirstChar = 50; SourceInt = SourceInt - 200; break;
case 1 : FirstChar = 49; SourceInt = SourceInt - 100; break;
case 0: FirstChar = 48; break;
}
switch (SourceInt/10){
case 9 :SecondChar = 57; SourceInt = SourceInt - 90; break;
case 8 :SecondChar = 56; SourceInt = SourceInt - 80; break;
case 7 :SecondChar = 55; SourceInt = SourceInt - 70; break;
case 6 :SecondChar = 54; SourceInt = SourceInt - 60; break;
case 5 :SecondChar = 53; SourceInt = SourceInt - 50; break;
case 4 :SecondChar = 52; SourceInt = SourceInt - 40; break;
case 3 :SecondChar = 51; SourceInt = SourceInt - 30; break;
case 2 :SecondChar = 50; SourceInt = SourceInt - 20; break;
case 1 :SecondChar = 49; SourceInt = SourceInt - 10; break;
case 0 :SecondChar = 48; break;
}
switch (SourceInt){
case 9: ThirdChar= 57; break;
case 8: ThirdChar= 56; break;
case 7: ThirdChar= 55; break;
case 6: ThirdChar= 54; break;
case 5: ThirdChar= 53; break;
case 4: ThirdChar= 52; break;
case 3: ThirdChar= 51; break;
case 2: ThirdChar= 50; break;
case 1: ThirdChar= 49; break;
case 0: ThirdChar= 48; break;
}
ResultInChars[0] = FirstChar;
ResultInChars[1] = SecondChar;
ResultInChars[2] = ThirdChar;
ResultInChars[3] = ' ';
ResultInChars[4] = NULL;
ResultInChars[5] = NULL;
ResultInChars[6] = NULL;
}
later i have used puts1USART with an array containing 3 above chars (FirstChar, SecondChar & ThirdChar)
i've also added a "lineNumber" before every 4 values and CrLf after the forth value
and it has resulted an output looks like that.....
output to terminal in Putty over Serial port
and its working......
thanks for your help
I would apriciate your suggestions,
Guy

Sorting too slow

So, I'm doing a project for my programming languages class, and i have to create a structure, sort it, and then show the time it takes to do it, the thing is bubble sorting(case 1) takes 60 sec to do it, insertion(case 2) 5 sec and selection (case 4) takes 10 sec. All this sorting 100000 elements. shell only takes 0.03 so i started thinking i might have something wrong with my algorithms. can some one help me?
void ordenesc(compleja * vd, int tam)
{
int i=0,j=0,k=0,aux=0,op=0,inc=0,minimo=0;
char auxcad[20];
clock_t start, end;
double tiempo;
op=menus(3);
start = clock();
switch(op)
{
case 1://Burbujeo
for(i=1;i<=tam;i++)
{
for(j=0;j<tam-1;j++)
{
if(vd[j].nro>vd[j+1].nro)
{
aux=vd[j].nro;
vd[j].nro=vd[j+1].nro;
vd[j+1].nro=aux;
strcpy(auxcad,vd[j].cad);
strcpy(vd[j].cad,vd[j+1].cad);
strcpy(vd[j+1].cad,auxcad);
}
}
}
break;
case 2://Inserccion
for(i = 1; i < tam; i++)
{
aux=vd[i].nro;
strcpy(auxcad,vd[i].cad);
for (j = i - 1; j >= 0 && vd[j].nro > aux; j--)
{
vd[j+1].nro=vd[j].nro;
strcpy(vd[j+1].cad,vd[j].cad);
j--;
}
vd[j+1].nro=aux;
strcpy(vd[j+1].cad,auxcad);
}
break;
case 3://Shell
inc=(tam/2);
while (inc > 0)
{
for (i=0; i < tam; i++)
{
j = i;
aux = vd[i].nro;
strcpy(auxcad,vd[i].cad);
while ((j >= inc) && (vd[j-inc].nro > aux))
{
vd[j].nro = vd[j - inc].nro;
strcpy(vd[j].cad,vd[j-inc].cad);
j = j - inc;
}
vd[j].nro = aux;
strcpy(vd[j].cad,auxcad);
}
if (inc == 2)
inc = 1;
else
inc = inc * 5 / 11;
}
break;
case 4://Seleccion
for(i=0;i<tam-1;i++)
{
minimo=i;
for(j=i+1;j<tam;j++)
{
if(vd[minimo].nro > vd[j].nro) minimo=j;
}
aux=vd[minimo].nro;
vd[minimo].nro=vd[i].nro;
vd[i].nro=aux;
strcpy(auxcad,vd[minimo].cad);
strcpy(vd[minimo].cad,vd[i].cad);
strcpy(vd[i].cad,auxcad);
}
break;
case 9:
break;
default:
break;
}
end = clock();
tiempo = ((double) (end - start)) / CLOCKS_PER_SEC;
//system("cls");
i=0;
for(i=0;i<tam;i++){
printf("%d %s \n",vd[i].nro,vd[i].cad);}
printf("\n Tardo %f segundos \n", tiempo);
return;
}
P.d:Edited the text sorry for my english is not my first language and my brain is failing due to this.
To make sure your sort algorithm works as expected, you could add a check to the final loop that the elements are actually ordered when you print them. Its relatively unlikely that there is a fundamental error in the algorithm and it still sorts correctly.
One point of the exercise may be to show that sorting algorithms really matter, and selection sort is the only algorithm that has a better performance than O(n^2) in your list. So I wouldn't be too surprised by wide differences in performance.
One improvement you could make to bubble sort is that you only need to iterate over i elements in the inner loop (instead of tam), as the i-largest element will have bubbled up all the way in the inner loop.
Another improvement may be to just copy the pointers instead of the contents of the char arrays, e.g.
instead of
char auxcad[20];
...
strcpy(auxcad, vd[j].cad);
strcpy(vd[j].cad, vd[j+1].cad);
strcpy(vd[j+1].cad, auxcad);
you may want to write
char* auxcad;
...
auxcad = vd[j].cad;
vd[j].cad = vd[j+1].cad;
vd[j+1].cad = auxcad;

SDCC integer comparison unexpected behavior

I'm trying to get started on a project using a PIC18F24J10. Attempting to use SDCC for this. At this point I've reduced my code to simply trying to nail down what is happening, as I've been seeing bizarre behavior for a while now and can't really proceed until I figure out what is going on. Not sure if this is my only problem at this point, but I have no idea why this is happening. Timer interrupt fires off, coupled with a #defined for loop causes LEDs on PORTC to blink maybe twice a second. If I just do a PORTC=0xFF and PORTC=0 this works fine. But it gets weird when I try to go much beyond that.
...
#define _RC0 31
#define _RC1 32
#define _RC2 33
#define _RC3 34
#define _RC4 35
#define _RC5 36
#define _RC6 37
#define _RC7 38
#define HI 1
#define LO 0
void why(unsigned char p, int z)
{
if(z == HI)
{
if(p == _RC0) PORTCbits.RC0 = 1;
else if(p == _RC1) PORTCbits.RC1 = 1;
else if(p == _RC2) PORTCbits.RC2 = 1;
else if(p == _RC3) PORTCbits.RC3 = 1;
else if(p == _RC4) PORTCbits.RC4 = 1;
else if(p == _RC5) PORTCbits.RC5 = 1;
else if(p == _RC6) PORTCbits.RC6 = 1;
else if(p == _RC7) PORTCbits.RC7 = 1;
}
else if(z == LO)
{
PORTC = 0b11110000;
}
else
{
PORTC = 0b10101010;
}
}
void timer_isr (void) __interrupt(1) __using (1)
{
TMR0H=0;
TMR0L=0;
why(_RC0, LO);
why(_RC1, LO);
why(_RC2, LO);
WAIT_CYCLES(5000);
why(_RC0, HI);
why(_RC1, HI);
why(_RC2, HI);
WAIT_CYCLES(5000);
}
void main(void)
{
WDTCONbits.SWDTE = 0;
WDTCONbits.SWDTEN = 0;
TRISC = 0b00000000;
PORTC=0b00000000;
INTCONbits.GIE = 1;
INTCONbits.PEIE = 1;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
T0CONbits.T08BIT = 0;
T0CONbits.T0CS = 0;
T0CONbits.PSA = 1;
TMR0H = 0;
TMR0L = 0;
T0CONbits.TMR0ON = 1;
while(1)
{
}
}
In the code above, four of the LEDs should blink, while the other four stay on. Instead, the LEDs stay on in the 10101010 pattern that happens in the "else" block, which should happen when "why" is called with any value other than HI or LO. I never call it with anything else, so why does it ever reach that?
UPDATE - Further reduction, no more interrupts or unspecified includes/defines. This is now the entirety of the program, and I am still seeing the same behavior. Changed the pattern from 10101010 to 10101011 so that I could verify the chip is actually being programmed with the new code, and it appears to be.
#include "pic16/pic18f24j10.h"
#define WAIT_CYCLES(A) for(__delay_cycle = 0;__delay_cycle < A;__delay_cycle++)
int __delay_cycle;
#define HI 1
#define LO 0
void why(int z)
{
if(z == HI)
{
PORTC = 0b11111111;
}
else if(z == LO)
{
PORTC = 0b11110000;
}
else
{
PORTC = 0b10101011;
}
}
void main(void)
{
TRISC = 0b00000000;
PORTC=0b00000000;
while(1)
{
why(LO);
WAIT_CYCLES(5000);
why(HI);
WAIT_CYCLES(5000);
}
}
Once the interrupt is asserted it is never cleared. That results in timer_isr() being called repeatedly. No other code is ever reached. The TMR0IF bit must be cleared in software by the Interrupt Service Routine.
Keep in mind you not only need to spend less time in the ISR than the period of the timer - it’s a good practice to spend the minimum amount of time necessary.
Remove the delays and simply toggle a flag or increment a register. In your main while (1) loop monitor the flag or counter and make your calls to why() from there.

How do I modify the Min Heap insert and delete function to accept a second comparison if the primary comparison happens to be equal?

below I have a standard insert and delete function for a Min Heap, what I need to do is add a special case to both the functions when the T.num comparison happen to be equal, I then need to then compare the T.Letter where the lower Ascii value is popped first. Without the comments is the standard insert and delete, add the commented section would be my attempt to add the new feature, which, for the life of me, I don't understand why it wouldn't work.
void MinHeap<T>::insert(T& e)
{
int CurrNode = ++HeapSize;
while(CurrNode != 1 && heap[CurrNode/2].num >= e.num)
{
/*
if(heap[CurrNode/2].num == e.num)
if(heap[CurrNode/2].letter <= e.letter)
break;
*/
heap[CurrNode] = heap[CurrNode/2];
CurrNode /= 2;
}
heap[CurrNode] = e;
}
void MinHeap<T>::delet()
{
T LastNode = heap[HeapSize--];
int CurrNode = 1;
int child = 2;
while(child <= HeapSize)
{
if(child < HeapSize && heap[child].num >= heap[child+1].num)
{
/*
if(heap[child].num == heap[child+1].num)
if(heap[child].letter <= heap[child+1].letter)
child--;
*/
child++;
}
if(LastNode.num <= heap[child].num)
{
/*
if (LastNode.num == heap[child].num)
{
if (LastNode.letter <= heap[child].letter)
break;
}
else
*/
break;
}
heap[CurrNode] = heap[child];
CurrNode = child;
child *= 2;
}
heap[CurrNode] = LastNode;
}
You can simply overload the comparison operator for the T type, like this:
bool operator >(const T &left, const T &right) {
return left.num > right.num ||
left.num == right.num && left.letter <= right.letter;
}
And then replace heap[CurrNode/2].num >= e.num with heap[CurrNode/2] > e.
It is best to avoid code such as that in your commented sections because it can quickly get confusing to follow and debug.

Resources